Player still gets kicked despite being on whitelist

This code is so when slock is on, only whitelisted people can get in without being kicked. When testing different users, they are still being kicked despite being whitelisted. Can someone help me out?

Code:
serverlock = false

local Whitelisted = {"KaylaPls","909zenka"}
local Remote = game.ReplicatedStorage.SlockUnslock

Remote.OnServerEvent:Connect(function(Player,LockVal)
	wait()
	print(LockVal)
	if LockVal == "Locked" then
		serverlock = true
	end
	
	if LockVal == "Unlocked" then
		serverlock = false
	end
	
end)
game.Players.PlayerAdded:Connect(function(Player)
	if serverlock == true then
		for i = 1, #Whitelisted do
			print(Whitelisted[i])
			if Player.Name == Whitelisted[i] then
				print("Player is whitelisted")
				break
			elseif Player.Name ~= Whitelisted[i] then
				Player:Kick("This event hasn't started yet!")
			end
		end
	end
end)
1 Like

It’s not necessary to put an elseif when you can just put else instead so this script should probably be correct

local Whitelisted = {"KaylaPls","909zenka"}
local Remote = game.ReplicatedStorage.SlockUnslock

Remote.OnServerEvent:Connect(function(Player,LockVal)
	wait()
	print(LockVal)
	if LockVal == "Locked" then
		serverlock = true
	end
	
	if LockVal == "Unlocked" then
		serverlock = false
	end
	
end)
game.Players.PlayerAdded:Connect(function(Player)
	if serverlock == true then
		for i = 1, #Whitelisted do
			print(Whitelisted[i])
			if Player.Name == Whitelisted[i] then
				print("Player is whitelisted")
				break
			else
				Player:Kick("This event hasn't started yet!")
			end
		end
	end
end)
1 Like

The code could be simplified a little bit:

game.Players.PlayerAdded:Connect(function(Player)
	if serverlock and table.find(Whitelisted, Player.Name) == nil then
		Player:Kick("This event hasn't started yet!")
	end
end)
1 Like

Firstly, use Roblox IDs, not usernames. Secondly, you can’t use

You need an else statement, not an elseif statement.

The main issue is that you used break after the If statement. Using break, you ended the statement and break is not necessary.

Please learn proper Lua syntax before you try to experiment or use for loop codes off the internet.

1 Like

No that’s not the issue mainly. Yes it is an issue but her main problem is the break statement.