The output says: attempt to index global 'gui' a nil value.
Is there a way to stop this from happening or is there another way to get PlayerGui
?
If I put the variables inside then the frame shows up but then it errors for the textbox saying attempt to index global 'h' a nil value for later lines.
game.Players.PlayerAdded:connect(function(p) gui = p:WaitForChild("PlayerGui") end) h = gui:WaitForChild("WOAH"):FindFirstChild("TextBox") f = gui:WaitForChild("WOAH"):FindFirstChild("Frame")
I'm sure that they are all in playergui so is there anyway to fix this?
The script tries to find h and f before the player has actually spawned because the event hasn't fired yet. Put the h and f variables inside of the player function. But since you cannot simply put your entire code inside of this event, we'll have to use the function GetPlayers
to put all the players into a table. I'll then go through each of those players and put their gui information into another table.
local playerlist = game:GetService("Players"):GetPlayers() local listofgui = {} function updateGui() playerlist = game:GetService("Players"):GetPlayers() --Updates the old list listofgui = {} --Wipes data for _,p in pairs(playerlist) do local gui = p:WaitForChild("PlayerGui") local h,f = gui:WaitForChild("WOAH"):FindFirstChild("TextBox"),gui:WaitForChild("WOAH"):FindFirstChild("Frame") listofgui[p.Name] = {h,f} --Saves in the listofgui end end game.Players.PlayerAdded:connect(function(p) updateGui() end) game.Players.PlayerRemoving:connect(function(p) updateGui() end) print(listofgui["PlayerName"][1].Name) --Print's out the name of h, or the name of the text box for that player. You'd put 1 for the h and 2 for the f.
Hope it helps!
The best thing to do is to wait until gui
exists:
game.Players.PlayerAdded:connect(function(p) gui = p:WaitForChild("PlayerGui") end) while not gui do wait(0.2) end --if 'gui' is nil, then wait and check again h = gui:WaitForChild("WOAH"):FindFirstChild("TextBox") f = gui:WaitForChild("WOAH"):FindFirstChild("Frame")
I hope this helped!
You have to wait until a value is not nil before you can get its children.
Try this:
game.Players.PlayerAdded:connect(function(p) gui = p:WaitForChild("PlayerGui") h = gui:WaitForChild("WOAH"):FindFirstChild("TextBox") f = gui:WaitForChild("WOAH"):FindFirstChild("Frame") end)
You should only use gui, h, and f inside game.Players.PlayerAdded:connect(function(p) end) because gui relies on p existing. h and f relies on gui existing.
Also TextBox and Frame shouldn't be direct children of PlayerGui they should be in a ScreenGui or equivalent.