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

Can someone tell me what is wrong here?

I'm very good at stupid mistakes and it's rather late so this may be a bit dumb. I am trying to make a script which only allows certain users to see a GUI. It works and tells me that my name is not equal to the first one on the list but it fails on the second. Here is the console output:

One-click random building placer loaded
22:08:47.000 - Unable to load plugin icon. Image may have an invalid or unknown format.
22:08:47.016 - Unable to load plugin icon. Image may have an invalid or unknown format.
22:08:47.267 - Workspace.Script:4: attempt to index global 'player' (a nil value)
22:08:47.268 - Stack Begin
22:08:47.268 - Script 'Workspace.Script', Line 4
22:08:47.268 - Stack End
{Increated Inc}
Name = Player Should Be: Increated
22:08:48.323 - Players.Player.PlayerGui.Increated Inc.LocalScript:13: attempt to index field '?' (a nil value)
22:08:48.324 - Stack Begin
22:08:48.324 - Script 'Players.Player.PlayerGui.Increated Inc.LocalScript', Line 13
22:08:48.325 - Stack End
22:08:48.912 - Attempt to connect failed: Passed value is not a function
22:08:48.912 - Script 'Players.Player.Backpack.DecoyDeploy.DecoyDeployScript', Line 93
22:08:48.913 - Stack End

And Here is the code:

-- Increated Inc | 29/10/15
-- Gui Authorisation Script
print("{Increated Inc}")
-- Config
guipath = script.Parent.Frame
authorised = {"Increated", "Player"}
-- /Config

players = game.Players:GetChildren()

for i=1, #players do
	for i = 1,#authorised do 
		if players[i].Name == authorised[i] then
		guipath.Visible = true
		break
	else
		print("Name = "..players[i].Name.." Should Be: "..authorised[i])
		guipath.Visible = false
	end
	end	
end

1 answer

✓ Accepted answer

You're using two for loops with the same index variable.

for i=1,#players do
	for i=1,#authorized do -- i is overwritten here

You're also going to print on line 17 for every player whose name isn't that particular index of the table until you reach the one that is.

local guipath=script.Parent.Frame
local authorised={"Increated","Player"}
local players=game.Players:GetPlayers()
for i=1,#players do
	guipath.Visible=false
	for j=1,#authorised do
		if players[i].Name==authorised[j]then
			guipath.Visible=true
			break
		end
	end
	if not guipath.Visible then
		print("Name = "..players[i].Name.." Should Be: "..authorised[i])
	end
end

A better solution would be to not use numerical indices:

local guipath=script.Parent.Frame
local authorised={Increated=true,Player=true}
local players=game.Players:GetPlayers()
for i=1,#players do
	guipath.Visible=authorised[players[i]]
	if not guipath.Visible then
		print("Name = "..players[i].Name.." Should Be: "..authorised[i])
	end
end

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