Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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)

2 comments

0

Yes this worked, Thanks

0

np

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

4 comments

0

attentive!

0

See thats exactly what I did try, before using return! It didn't work at all after that and kicked all the players

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

0

You're correct, I didn't pay attention to that aspect when I was writing it, Arsubia's answer will work just fine

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.

1 comment

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

This discussion is preserved as it was. New answers, comments, and votes are closed.