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

Help with an admin door, help with admins table?

Asked by
FiredDusk 1466 Moderation Voter
7 years ago

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)
0
try elseif hit.Parent.Name ~= v instead of else theawesome624 100 — 7y

2 answers

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

I see your problem.

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)

What this does,

This will call a function with the parameter "name". It will loop through the table, and if it finds a value in the table that matches the name, it returns true, which stops the function. If it gets through the table and no admins exist, it returns false.

Good Luck!

0
How can I do this without the function? I am asking this because I am not good with 'return' and I don't understand them. Please try to make it w/o a function if you can. FiredDusk 1466 — 7y
Ad
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

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

Answer this question