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

Why does it say plr is a nil value?

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.

2 comments

0

That wont work, but for your direct question, you still need to define "plr" like plr = game.Players.LocalPlayer. I'm assuming its a local script since you used OnClientEvent and there's a regular script firing the clients? What event is supposed to occur?

0

It enables a gui.

2 answers

✓ Accepted answer

#OnClientEvent; No Player 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. #Fix

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.

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