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

Why is this simple code not working?

Asked by
R_alatch 394 Moderation Voter
9 years ago

I'm using this code to find a gui, but it isn't working. I do get an error, here it is. ServerScriptService.Script:12: attempt to index global 'gui' (a nil value)

01function findgui()
02    for i, v in pairs(game.Players:GetPlayers()) do
03        local gui = v.PlayerGui:FindFirstChild("ScreenGui").TextBox
04    end
05end
06 
07while wait() do
08    gui = findgui()
09    if game.Players.NumPlayers >= 2 then
10        gui.Text = "Starting the Fight."
11    else
12        gui.Text = "Waiting for 2 or more players."
13        wait()
14    end
15end

3 answers

Log in to vote
1
Answered by 9 years ago

Well first of all in the function findgui() you are defining locally "gui". You should try and return a table of the guis instead, or use RemoteEvent:FireAllClients(), but you're not doing that, so err, another thing is you're trying to define gui = findgui() which doesn't return anything.

I have revised your code for it to work, if you want to know how something works please PM me on ROBLOX.

01function FindGuis()
02    local guis = {}
03    for _,plr in next, game.Players:GetPlayers() do
04        table.insert(guis, plr.PlayerGui.ScreenGui.TextBox)
05    end
06    return guis
07end
08 
09while wait() do
10    for _,gui in next, FindGuis() do
11    if game.Players.NumPlayers >= 2 then
12        gui.Text = "Starting the fight"
13    else
14        gui.Text = "Waiting for 2 or more players"
15        wait()
16    end
17end

I hope you can learn from this.

Ad
Log in to vote
0
Answered by
Spoookd 32
9 years ago

I think it is because gui is local in the findgui() function so it will only be recognized in there. Maybe try ending the function after the while loop or don't make the gui local.

0
The answers down below are pretty much what I just said :/ Spoookd 32 — 9y
Log in to vote
0
Answered by 9 years ago

Your issue here lies within the scope of the function it's a very simple fix!

01function findgui()
02    for i, v in pairs(game.Players:GetPlayers()) do
03        local gui = v.PlayerGui:FindFirstChild("ScreenGui").TextBox --Variable is only called within this function
04    end
05end
06 
07while wait() do
08    gui = findgui()
09    if game.Players.NumPlayers >= 2 then
10        gui.Text = "Starting the Fight."
11    else
12        gui.Text = "Waiting for 2 or more players."
13        wait()
14    end
15end

You will want to tear the local gui variable out of the function so it can be read by the entire script as such.

01local gui = v.PlayerGui:FindFirstChild("ScreenGui").TextBox
02function findgui()
03    for i, v in pairs(game.Players:GetPlayers()) do
04  --Call gui variable and manipulate it as you please for the player
05    end
06end
07 
08while wait() do
09    gui = findgui() --Variable needs a change, and I don't believe you can bind a variable to a function anyways
10    if game.Players.NumPlayers >= 2 then
11        gui.Text = "Starting the Fight."
12    else
13        gui.Text = "Waiting for 2 or more players."
14        wait()
15    end
16end

But looking at this script, the scope isn't the only issue. If this is in a local script, the for i,v in pairs loops is unnecessary because you can call the local player directly.

If this is in a server script, it won't work period because only local scripts can write to the PlayerGui, in which case you will need close to a full re-write.

If you have any other questions feel free to Roblox PM me or ask in the comments!

~MBacon15

Answer this question