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

Why does this i, v in pairs only see the first string in the table?

Asked by 4 years ago
Edited 4 years ago
game.Players.PlayerAdded:Connect(function(player)
    local bruh = {"iiConstable_Subwayx","Censored my friends name"} -- Thats not his actual name
    for i, v in pairs(bruh) do
        if player.Name == v then
            return nil
            else
            player:Kick('This game is currently only open to ' .. bruh[1] .. ' and, ' .. bruh[2] .. '. This is still a work in progress')
        end
    end
end)

Alright, if I try this, it only works for the name in table[1], For example, if I put my friend's name there and my name after, it's broken for me and kicks me, however, it would work for him, any ideas on why and how I could fix it?

3 answers

Log in to vote
1
Answered by 4 years ago

well, your game kicks anyone in the table who's name matches one of the names in the array..

do this:

local bruh = {};
game.Players.PlayerAdded:Connect(function(player)
    for _, name in pairs(bruh) do
        if(name:lower() == player.Name:lower()) then
            return
        end
    end

    player:Kick("Your not allowed in")
end)
0
Yes this worked, Thanks iiConstable_Subwayx 130 — 4y
0
np User#23252 26 — 4y
Ad
Log in to vote
1
Answered by
Nanomatics 1160 Moderation Voter
4 years ago

Your problem is that you're checking the player's names, if it is in that table you defined, you want it to do nothing, why are you using return? Return stops that part from going on further.

To clarify what I want to say here is how return works in your case, once it finds one player that meets the condition it would stop, and won't check further

So what I suggest you do is

if player.Name ~= v then
    player:Kick('This game is currently only open to ' .. bruh[1] .. ' and, ' .. bruh[2] .. '. This is still a work in progress')
end

Instead of what you already have, and it should go through all players in the table

Hope I helped, if you have further questions please let me know

0
attentive! User#23252 26 — 4y
0
See thats exactly what I did try, before using return! It didn't work at all after that and kicked all the players iiConstable_Subwayx 130 — 4y
0
Ok, I see why this didnt work, I tried using print(player.Name, v) to see the problem and it was just that it checked if it was my name and worked, therefore it didnt check for the other strings in the table and thats why return worked. If I use ~= it checks if my name is the first one AND the second which is impossible unless both strings are the same iiConstable_Subwayx 130 — 4y
0
You're correct, I didn't pay attention to that aspect when I was writing it, Arsubia's answer will work just fine Nanomatics 1160 — 4y
Log in to vote
0
Answered by
Tokyo7979 131
4 years ago

Try replacing the [1] and [2] with i instead.

bruh[i]

Do this so the loop runs EVERYTHING avaliable in the table. You'll have to eventually add more text to your message if you want to add more people to your whitelist though.

0
Ok, But what would this change? The bruh[1] and bruh[2] is for the kick message which has nothing to do with my problem iiConstable_Subwayx 130 — 4y

Answer this question