Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does it say plr is a nil value?

Asked by
Jexpler 63
5 years ago

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.

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? ABK2017 406 — 5y
0
It enables a gui. Jexpler 63 — 5y

2 answers

Log in to vote
2
Answered by 5 years ago

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)
Ad
Log in to vote
0
Answered by
angeI1001 123
5 years ago
Edited 5 years ago

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.

Answer this question