When I touch a part, it prints both and my name is FiredDusk in studio mode...
Admins = { "Player1", "FiredDusk" } script.Parent.Touched:connect(function(hit) for i,v in pairs(Admins) do if hit.Parent.Name == v then print'YAY' else print'BOO' end end end)
Let me explain the problem before giving you the answer.
If an admin touches the part, it will print Yay
, but it will also print Boo
. This is because you're not stopping the loop when an admin shows up. Even if you do stop the loop, the Admin may not be first in the list, so it will still print Boo
. I would recommend using a function to check instead, as it will be much easier.
Example,
Admins = { "Player1", "FiredDusk" } local function CheckAdmin(name) for i,v in pairs(Admins) do if name == v then return true; end end return false; end script.Parent.Touched:connect(function(hit) if CheckAdmin(hit.Parent.Name) then print("Yay") else print("Boo") end end)
Good Luck!
Your script works perfectly fine, it's only showing both because Player1
is in the table.
Your code loops through each variable of the table, adn then decides whether or not hit.Parent.Name
is equivalent to that. Since there is nobody name Player1 in your game, it will print BOO
everytime that the brick is touched because it has to go through the entire table.
You can either remove the BOO
part, or remove Player1 from your table. Both of these will fix your problem