Im trying to update some UI based on a value in a database, I am doing this as part of the ‘playerConnecting’ event, I am unsure how to get the target client from source, as passing source directly does not seem to be working.
If it triggered the event at all it would print a line to the console, however no line is being printed.
Any assistance with this would be greatly appreciated.
Hope this one is a bit useful, this is a small code i wrote (SERVER SIDED!)
which i use to trigger client scripts based on their PlayerName (we use this a closed member server for developing only so it’s perfect to use member names for me there for several testing purposes but you can easily adapt it to return the player id when you compare their steamid in my loop instead
function GetPlayerIdByName(PlayerName)
ptable = GetPlayers()
PiD = -1000
for _, i in ipairs(ptable) do
local name = string.upper(GetPlayerName(i))
if(name == string.upper(PlayerName)) then
PiD = i
break
end
end
return PiD
end
When using it including error handling i just use it like this:
-- NOTE: Param2 is a parameter from MY script of course ;)
PiD = tonumber(GetPlayerIdByName(Param2))
if PiD == -1000 then
print("Server: Sorry but it seems that " .. Param2 .. " isn't online at the moment.")
else
-- Do your stuff here
end
BUT the other ‘issue’ which i see here is that i PERSONALLY would recommend to trigger such client events on player spawn, since not all scripts might (or even ARE) loaded properly during connecting, and not all variables might respond like you might expect when doing it on player connecting.
And then letting that CLIENT script ‘ask’ the server to update the client with ‘all the player info’ needed on spawn
Thank you, I shall try this soon. The issue i see with running it on playerspawn is the fact as far as I am aware that event is called every time the player spawns, so after death and such, that could be problematic
local FirstTimeSpawn = true
AddEventHandler('playerSpawned', function(spawn)
if FirstTimeSpawn then
-- Do your stuff here
FirstTimeSpawn = false
end
end)
Just one of the solutions possible
NOTE!: Not 100% certain if that was the exact spawn handler though! since i’m typing it ‘blindly’ here now from memory like all the times i’ve used it like that haha
I opted for the playerSpawned option as I thought it would probably just be a better idea overall and getting the client to tell the server it wants the data. Works perfectly, thanks for your help.
I marked your first post as the solution as that is a solution to the problem I actually asked about.