Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why is player nil when passed from a remote event?

From server:

game.Players.PlayerAdded:connect(function(player)
	remotes.addLabel:FireAllClients(player)
end)

From local script:

function addLabel(player)
	print(player.Name)
end

Error:

Player1
17:02:07.346 - Players.Player1.PlayerGui.listScript:20: attempt to index local 'player' (a nil value)

Why is this not working? If I pass in the Player's name from the server, it'll work though. But why can't I pass in the player object and have it working? I tested this in STUDIO's server testing tool.

What's going on?

2 comments

0

I think you got it backwards or something. The local script part should be a server script, and the server script looks like it should be in the local script. I dont know if this is the problem though

0

He's firing a client side function from the server

2 answers

Working a-okay;

game.Players.PlayerAdded:connect(function(player)
	remotes.addLabel:FireAllClients(player);
end)
remotes.addLabel.OnClientEvent:connect(function(player)
	print(player.Name);
end)

Edit: Since the entire point of the RemoteEvent is to enable filtering, it seems utterly ironic that I'd miss this. That said, the player isn't replicated from the server to the client, so the client receives the Player Object, but only the Player Object, not any of it's properties. Passing player.Name to FireAllClients and printing the parameter giving to OnClientEvent generates the desired output.

game.Players.PlayerAdded:connect(function(player)
	remotes.addLabel:FireAllClients(player.Name);
end)
remotes.addLabel.OnClientEvent:connect(function(name)
	print(name);
end)

2 comments

0

Nope. Doesn't work. It will work with one player but in online mode when one player connects and then another, a nil message is shown. Why?

0

You're correct. Answer edited.

Hello Arithmeitcity!

I fixed your script, and I think now this will work.

game.Players.PlayerAdded:Connect(function(player)
	remotes.addLabel:FireAllClients()
end)
remotes.addLabel.OnClientEvent:Connect(function(player)
	print(player.Name)
end)

I hope I helped you :)

This discussion is preserved as it was. New answers, comments, and votes are closed.