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

What am I doing wrong with this table?

Asked by 9 years ago

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.

0
edited. rexbit 707 — 9y

2 answers

Log in to vote
1
Answered by
rexbit 707 Moderation Voter
9 years ago

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

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. Zannorian 25 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

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

Answer this question