Setting and syncing client/server values on an entity

Hi, I’m new to FiveM programming. I was wondering what the method of sending values attached to entities to the client.

The reason I’m asking is that I am working on an RP Name script, the server gets the name from the database, but I need to find a way to network the name (attached to the entity) to all clients.

Any help is appriciated.
Cheers.

1 Like

Decors
http://runtime.fivem.net/doc/reference.html#_0x05661B80A8C9165F

1 Like

I can’t see any function here that supports strings though, just bools and integers.

Like a first and last name? Or like an entity name?

A first and last name type system, I guess you could convert the string into numbers but I have no idea how todo that then convert it back on the client.

Make a server sided table and save the first and last name to their server id.

Yes, but how could I network their name to all clients?

Yes. You can definitely do that.

Yes, but how could I?

Do you know what a table is? And do you know how to insert data into those table? And do you also know how to use a loop to search through the table and return data?

Yes, but I’m asking how to network string values onto an entity.

You can’t. You will need to convert the string to some integer form and then convert back to a string on the other end.

What @xander1998 is suggesting (I think) is you can instead store the names of each player in a table. You could then store the index ID of that table as a decorator on the player. When reading the decorator, just fetch the table row for the name.

Yeah, I though about doing that, but how can I send the clients the names from the server (via SQL database).

You would need some mysql resource (like the mysql async one) to fetch the information on the server side.

What I would do is have a table on the server side and fetch the information when a player joins. Then remove the row when the player quits. I would also have a triggerclientevent going to all clients to update the table on the clients, too.

Whenever the name is needed, just reference that table on the client, so you don’t need to spam with triggerserverevents to get the name from the server every time you need it.

1 Like

THIS IS JUST KINDA AN EXAMPLE. DO NOT COPY AND PASTE BECAUSE IT WILL NOT WORK. THIS IS FOR A VISUAL REP. THIS IS ALSO NOT TESTED

--[[ SERVER ]]--
-- Global Table --
player_data = {}
plater_data = setmetatable(player_data, {})

function GetPlayerData(id)
	local result = {}
	for i = 1, #player_data do
		if player_data[i].id == id then
			table.insert(result, {first = player_data[i].firstname, last = player_data[i].lastname})
		end
	end

	return result
end

function AddPlayerData(id, first, last)
	table.insert(player_data, {plyid = id, firstname = first, lastname = last})
end

function RemovePlayerData(id)
	for i = 1, #player_data do
		if player_data[i].id == id then
			player_data[i] = nil
		end
	end
end

-- Adding Player Data To Table --
RegisterServerEvent("PlayerData:AddPlayerData")
AddEventHandler("PlayerData:AddPlayerData", function()
	local id = source

	-- DO MYSQL RESULT --
	local result = MySQL.Sync.fetchAll("SELECT * FROM players WHERE steamid = @ident", {['@ident'] = GetPlayerIdentifiers(id)[1]}) -- STEAM ID MAY NOT ALWAYS BE 1!!!!

	AddPlayerData(id, result[1].first, result[1].last)
end)

-- Removing Player Data From Table --
AddEventHandler("playerDropped", function()
	local id = source
	RemovePlayerData(id)
end)

--[[ CLIENT ]]--

-- Adding Player To Table --
AddEventHandler("onClientMapStart", function()
	TriggerServerEvent("PlayerData:AddPlayerData")
end)

Maybe Try Something Like That?

You have to be using MySQL Async to use that type of MySQL Query.

2 Likes

Thanks for the help.

I understand and have written all the networking and server code, but when I network it to the player I want something like this to run.

AddEventHandler('RecieveRPDataBatch', function(player_data)
	local players = GetPlayers()
	for v,k in pairs(players) do
		if player_data[i].plyid == GetPlayerServerId(k) then
			k.RPname = player_data[i].rpname
		end
	end

end)

But, it seems like there is no way to loop through all the players on the client and store data to them? Any ideas?