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

Is there a way to make these variables work on the outside of the function?

Asked by 7 years ago
Edited 7 years ago

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?

0
In the very first line of the script, type "gui = nil" and tadaa! Problem is gone. superalp1111 662 — 7y

3 answers

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

What the problem is

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.


Final Product

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!

0
19:02:16.544 - PlayerRemoved is not a valid member of Players 19:02:16.545 - Stack Begin 19:02:16.545 - Script 'Workspace.GoToGameScript!', Line 7 19:02:16.546 - Stack End 19:02:16.546 - Workspace.GoToGameScript!:5: attempt to call global 'updateGui' (a nil value) 19:02:16.547 - Stack Begin 19:02:16.547 - Script 'Workspace.GoToGameScript!', Line 5 19:02:16.548 - Stack End arrowman888 69 — 7y
Ad
Log in to vote
1
Answered by 7 years ago

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!

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

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.

0
yeah, but my problem is I can't put my whole script inside of the playeradded thing so when I tried that the variable wouldn't go out. Is there another way to access a player gui in a regular script arrowman888 69 — 7y
0
Is this a Script or is it a LocalScript? shadownetwork 233 — 7y
0
Regular script arrowman888 69 — 7y

Answer this question