So I have a remote event which goes from server to client. it's this simple:
script.Parent.OnClientEvent:Connect(function(plr) plr.PlayerGui.Sol.Enabled = true end)
It says plr is a nil value.
The RemoteEvent.OnClientEvent
event does not pass the player as first parameter by default, unlike RemoteEvent.OnServerEvent
. The server actually fires OnClientEvent
, and the server isn't a player so that wouldn't work for that reason. Also you wouldn't need a player parameter for it as it's obvious it's working for the client listening for the event.
Declare your plr
variable at the top of the script, and done.
local plr = game.Players.LocalPlayer -- Assigning plr to the local player. script.Parent.OnClientEvent:Connect(function(...) -- If you would like, you can pass other things to the server. plr.PlayerGui.Sol.Enabled = true end)
A better solution:
local Player = game:GetService("Players").LocalPlayer -- // The service 'Players' night be nil, therefore, instead of looking for it, we create it if it's not been created yet. local PlayerGui = Player:WaitForChild("PlayerGui") -- // PlayerGui might also be nil, instead of calling it right away, we'll give it some time to find it. Default wait-time for WaitforChild() is 5 seconds unless you specify your own: (Thing:WaitForChild("Hello", 10) local Sol = PlayerGui:WaitForChild("Sol") -- // Might also be nil, it's better to wait instead of getting an unexpected delay that might throw an error if not found when you call it. script.Parent.OnClientEvent:Connect(function(...) Sol.Enabled = true end)
I hope this helped.