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

why am i getting a nill value when i fire this remote event?

--Server script--
game:GetService("Players").PlayerAdded:connect(function(player)
	wait(1)
	game.ReplicatedStorage.Event:FireClient(player)
end)
--local script--
local event = game.ReplicatedStorage.Event
event.OnClientEvent:connect(function(player)
	print(player.Name)
end)

why does it print 'nil' in the output?? please help.

1 answer

✓ Accepted answer

When firing a remote event from the server to the client, the only necessary argument is the Player object. However, this is only used in order for the server to know which client to specifically send the information to: which means that the argument is not actually passed to .OnClientEvent:Connect(function().

If you want to get the player on the client, you can either use Players.LocalPlayer:

Local Script:

--// Services
local ReplicatedStorage	= game:GetService("ReplicatedStorage")
local Players			= game:GetService("Players")

--// Service Objects
local Event 			= ReplicatedStorage:WaitForChild("Event")
local LocalPlayer		= Players.LocalPlayer

Event.OnClientEvent:Connect(function()
	print(LocalPlayer.Name)
end)

OR you can pass a second argument through the remote event, which is the same Player object:

Server Script:

--// Services
local ReplicatedStorage	= game:GetService("ReplicatedStorage")
local Players			= game:GetService("Players")

--// Service Objects
local Event 			= ReplicatedStorage:WaitForChild("Event")

Players.PlayerAdded:Connect(function(newPlayer)
	wait(1)
	Event:FireClient(newPlayer, newPlayer)
end)

Local Script:

--// Services
local ReplicatedStorage	= game:GetService("ReplicatedStorage")

--// Service Objects
local Event 			= ReplicatedStorage:WaitForChild("Event")

Event.OnClientEvent:Connect(function(LocalPlayer)
	print(LocalPlayer.Name)
end)

Either of these methods should work fine. Personally I would use the LocalPlayer method, because it means the RemoteEvent won't need to send extra information to the Client that the Client can easily get itself.

If you still need more help, feel free to ask! -Poe

1 comment

0

Thank You.

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