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

Why is my Remote Event reading "Player" as "nil"?

I have this In Pairs loop inside a script located on Workspace:

for i,Player in pairs(game.Players:GetPlayers()) do 
	--A bunch of code that isn't relevant to my problem
end

Inside the loop there's this line that refers a Remote Event that when fired should play music Client-Side:

game.ReplicatedStorage.PlayGameMusic:FireClient(Player)

This is the LocalScript on StarterGui that detects when the event is fired:

game.ReplicatedStorage.PlayGameMusic.OnClientEvent:Connect(function(Player)
	local ScreenGui = Player.PlayerGui:WaitForChild("ScreenGui")
	
	ScreenGui.Music:Play()
end)

But when testing the script In-Game, this shows up in the Output:

Players.Mordevifer.PlayerGui.RemoteFunctionReader:2: attempt to index nil with 'PlayerGui'

I really don't know why is the server sending nil, there's not a red line under Player, so that must mean that the code is in fact inside the loop, but is the problem originating on the loop or the script, my mind is blank right now so I would appreciate someone could help me in this (probably) dumb problem I'm having at the moment.

1 answer

✓ Accepted answer

when you do fireclient the first parameter is who (which player) you are sending it to. because you are doing fireclient, you can get the player by using localplayer instead.

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