[HELP] Not sure how to get target client from source in 'playerConnecting' event

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 :slight_smile:

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 :slight_smile:

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

Nope would not :slight_smile: just do it like this:

local FirstTimeSpawn = true

AddEventHandler('playerSpawned', function(spawn)
	if FirstTimeSpawn then
		-- Do your stuff here
		FirstTimeSpawn = false
	end
end)

Just one of the solutions possible :slight_smile:

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

Thats a good shout actually, thanks for your help bud. I’ll let you know if I have any success :slight_smile: That is indeed the correct event :stuck_out_tongue:

1 Like

Great haha wasn’t 100% sure, usually only post when i can copy from my code to know it’s “100% correct” haha

Hope you’ll get it sorted if not, just reply or msg me :slight_smile:

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.

1 Like

Thanks, appreciated and even more importantly: Glad it worked out for you and that i could be of help :slight_smile:

1 Like