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

What am I doing wrong with this table?

This is my first attempt with tables. So I am trying to make a script that when a player is added with a name from a table, a GUI will be visible on that users screen.

This is what I have so far (does not work ):

local users = {"Player"}
local children = game.Players:GetChildren()
function test()
if children.Name == users then
	children.PlayerGui.Test.Frame.Visible = true
else
	children.PlayerGui.Test:Destroy()
end
end
game.Players.PlayerAdded:connect(test)

All help will be very much appreciated.

1 comment

0

edited.

2 answers

✓ Accepted answer

The GetChildren() method is a table of the children from it's represented container. So if I said model:GetChildren() that would be a table of the children within it.

Generic Loops can, technically, break down the table to elements by using for as it's iterator, and one of it's 'most' commonly used function, pairs(). This receives all the values and combines it into one value.

local children=game.Players:GetPlayers() -- or getchildren

function test()
for index, values in pairs(children) do
values.PlayerGui.Test.Frame.Visible=true
else
values.PlayerGui.Test:Destroy()
end
end

game.Players.PlayerAdded:connect(test)

Edited

To make technical search if the player's name 'matches' any of the titles given in the array, we'd use, once again, a generic loop.

local names={"Player","Player1","Player2"}

function namecheck(player,names)
for index, value in pairs(names) do
if player.Name==value then
return true
end
return false
end

function test()
for _, values in pairs(game.Players:GetChildren()) do
if namecheck(values,names) then
values.PlayerGui.Test.Frame.Visible=true
else
values.PlayerGui.Test:Destroy()
end
end
end

1 comment

0

Thanks for the explanation but I am still a little confused on how make it check to see if the player name is a name from a table.

In line 4, you're checking if the player's name is equal to the whole table. What you want to do is check if the player's name is IN the table.

Here's a function that tells you if an element is in a table:

function find(tab, item)
	for i,v in pairs(tab) do
		if v == item then
			return true
		end
	end
	return false
end

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