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

Why is this simple code not working?

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)

function findgui()
	for i, v in pairs(game.Players:GetPlayers()) do
		local gui = v.PlayerGui:FindFirstChild("ScreenGui").TextBox
	end
end

while wait() do
	gui = findgui()
	if game.Players.NumPlayers >= 2 then
		gui.Text = "Starting the Fight."
	else
		gui.Text = "Waiting for 2 or more players."
		wait()
	end
end

3 answers

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.

function FindGuis()
	local guis = {}
	for _,plr in next, game.Players:GetPlayers() do
		table.insert(guis, plr.PlayerGui.ScreenGui.TextBox)
	end
	return guis
end

while wait() do
	for _,gui in next, FindGuis() do
	if game.Players.NumPlayers >= 2 then
		gui.Text = "Starting the fight"
	else
		gui.Text = "Waiting for 2 or more players"
		wait()
	end
end

I hope you can learn from this.

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.

1 comment

0

The answers down below are pretty much what I just said :/

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

function findgui()
    for i, v in pairs(game.Players:GetPlayers()) do
        local gui = v.PlayerGui:FindFirstChild("ScreenGui").TextBox --Variable is only called within this function
    end
end

while wait() do
    gui = findgui()
    if game.Players.NumPlayers >= 2 then
        gui.Text = "Starting the Fight."
    else
        gui.Text = "Waiting for 2 or more players."
        wait()
    end
end

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

local gui = v.PlayerGui:FindFirstChild("ScreenGui").TextBox
function findgui()
    for i, v in pairs(game.Players:GetPlayers()) do
  --Call gui variable and manipulate it as you please for the player
    end
end

while wait() do
    gui = findgui() --Variable needs a change, and I don't believe you can bind a variable to a function anyways
    if game.Players.NumPlayers >= 2 then
        gui.Text = "Starting the Fight."
    else
        gui.Text = "Waiting for 2 or more players."
        wait()
    end
end

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

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