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

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

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)

1 comment

0

Your variable 'gui' is directly searching for PlayerGui in the list of Players . :)

1 answer

✓ Accepted answer

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

2 comments

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'.

0

Thanks.

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