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 kick everyone and how do I fix it?

This script is supposed to allow the players with the ids in the table to join the game without getting kicked and having their chat removed. If their ID isn't in that table then they are supposed to be kicked and have their chat removed. However, when this is in my game, it kicks everyone who joins even the players with the IDs and it doesn't remove their chat. Please can someone help me?

local ids={14946581,-1,0,1}
game.Players.PlayerAdded:connect(function(plr)
local it = false
for _,v in pairs(ids) do
if plr.UserId==v then
local it=true
end
end
if it==false then
	game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.Chat, false)
	wait(.1)
	plr:Kick("You do not have permission to join this game.")

end
end)

1 comment

2

you define a new variable in the scope of the for loop and set it to true, the "it" variable in the player function scope is still false so it will kick all players.

1 answer

✓ Accepted answer

in line 6, by using local you are not changing the it in line 3, but rather creating your own separate it from the first one. Simply remove the local and you should be good to go!

local ids={14946581,-1,0,1}
game.Players.PlayerAdded:connect(function(plr)
local it = false
for _,v in pairs(ids) do
if plr.UserId==v then
it=true
end
end
if it==false then
    game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.Chat, false)
    wait(.1)
    plr:Kick("You do not have permission to join this game.")

end
end)

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