Ad
Log in to vote
0

For i, v loop doesn't stop where it should and just keeps on going even after an "else"?

Asked by
016er 9
1 month ago

Hello! I've got a bit of a problem with my ban command script, and I've narrowed it down to this function with the help of print debugging. My issue with this is that I'm trying to make it so that moderators within the game aren't able to ban eachother. However, this doesn't seem to work.

local function banByUserID(userid)
    local nameofUser = game.Players:GetNameFromUserIdAsync(userid)
    for i,v in pairs(moderators) do
        if v == nameofUser then
            print ("Nice try. /".. nameofUser.. "/ is a moderator and can't be banned!")
        else -- should stop at this once has detected a moderator
            if bansDataStore:GetAsync("banned_"..userid,1) then
                print ("User ".. nameofUser.. " has already been banned!")
            else
                if nameofUser then
                    bansDataStore:SetAsync("banned_"..userid,1)
                    if game.Players:FindFirstChild(nameofUser) then
                        game.Players:FindFirstChild(nameofUser):Kick(reason)
                    end
                    print ("User ".. nameofUser.. " has been banned!")
                end
            end
        end
    end
end

It should stop at the "else" once it has detected a moderator, but it keeps going on, resulting in this and banning the moderator anyway. image showing output

The "moderator" variable is just a regular table with strings of people's usernames.

1 answer

Log in to vote
0
Answered by 1 month ago

You can break a loop with the built in function 'break'

for i, v in pairs() do
print('_')
break
end
0
Can't believe I missed such an easy solution, thank you! 016er 9 — 1mo
0
You're welcome. TheEagle722 114 — 1mo
Ad

Answer this question