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

There says an error, that the :FindFirstChild() is a nil value?

Asked by
RootEntry 111
6 years ago

08:18:12.704 - Players.Player1.PlayerGui.main_Panel.admingui.warn.LocalScript:2: attempt to call method 'FindFirstChild' (a nil value) 08:18:12.704 - Stack Begin

Here is the code:

local player = game.Players:GetPlayers()
local gui = player:FindFirstChild("PlayerGui"):FindFirstChild("main_Panel")
local warnbutton = gui:FindFirstChild("admingui"):FindFirstChild("warn")
local typemessage = gui:FindFirstChild("admingui"):FindFirstChild("typemessage")
local warnmessage = gui:FindFirstChild("admingui"):FindFirstChild("warnmessage")

warnbutton.MouseButton1Click:connect(function()
    print("bruh")
end)
0
Your variable 'gui' is directly searching for PlayerGui in the list of Players . :) User#17685 0 — 6y

1 answer

Log in to vote
2
Answered by
DanzLua 2879 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

Ok lets start out by making sure we are doing this is a localscript, after that lets set up our player variable

local player = game.Players.LocalPlayer

After that we will set up our gui variables

local gui = player.PlayerGui:WaitForChild("main_Panel")--waitforchild so we make sure its there
local warnbutton = gui:WaitForChild("admingui").warn

Now for our messages

local typemessage = gui:WaitForChild("admingui").typemessage
local warnmessage = gui.warnmessage

Finally our event

warnbutton.MouseButton1Click:connect(function()
    print("bruh")
end)

Lets put it together,

local player = game.Players.LocalPlayer
local gui = player.PlayerGui:WaitForChild("main_Panel")
local warnbutton = gui:WaitForChild("admingui").warn
local typemessage = gui:WaitForChild("admingui").typemessage
local warnmessage = gui.warnmessage
warnbutton.MouseButton1Click:connect(function()
    print("bruh")
end)

In conclusion to get to the player you get the LocalPlayer from players, only works in localscript. Also you don't need to use findfirstchild for everything and unique named instances

0
+1. To add to this answer, the reason the error occurred is because :GetPlayers() returns a table of players, so "FindFirstChild" didn't exist in that table. Also, to explain DanzLua's last sentence, there's no point in using FindFirstChild if you don't confirm that it doesn't return 'nil'. chess123mate 5873 — 6y
0
Thanks. RootEntry 111 — 6y
Ad

Answer this question