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

Why the kick system isn't executed?

I'm working on a game with a friend. I test my scripts on a game, and i wanted it to be private, only my friend and me would be able to join.

When i put just my username, it works well.

game.Players.PlayerAdded:Connect(function(player)

local player = player.Name

  

if

player == "David0fficial_BR"

then

print("Acess Granted for "..player)

else

local plr = game.Players:FindFirstChild(player)

print("Acess Denied for "..player)

plr:Kick("You're not allowed to join the game.")

end

end)

But when i try to do it with two usernames, any other player is able to join

game.Players.PlayerAdded:Connect(function(player)

local player = player.Name

  

if

player == "David0fficial_BR" or "gustavomfmgame"

then

print("Acess Granted for "..player)

else

local plr = game.Players:FindFirstChild(player)

print("Acess Denied for "..player)

plr:Kick("You're not allowed to join the game.")

end

end)

I know that the error is in the usernames, but can someone explain why it doesn't work with two there?

1 comment

0

You're doing too many extra steps.

3 answers

✓ Accepted answer
local Users = {
	["Player1"] = true,
	["Player2"] = true,
	["Player3"] = true,
	["Player4"] = true,
	["Player5"] = true,
	["JakyeRU"] = true	
}

game.Players.PlayerAdded:Connect(function(player)
	if not Users[player.Name] then
		player:Kick("You are not allowed!")
	end
end)

The Script is checking everytime a player joins, if his username is in the table Users. If the player is not in the table with the value true it will automatically be false. So, when any player joins and he is not in the table he will get kicked. If he is in the table, nothing will happen.

If you want to do something when the player joins and he is in the table, use else.

local Users = {
	["Player1"] = true,
	["Player2"] = true,
	["Player3"] = true,
	["Player4"] = true,
	["Player5"] = true,
	["JakyeRU"] = true	
}

game.Players.PlayerAdded:Connect(function(player)
	if not Users[player.Name] then
		player:Kick("You are not allowed!")
	else
		print("Attention, " ..player.Name.. " is allowed!")
	end
end)

enter image description here

Here's how your Script should look like.

game.Players.PlayerAdded:Connect(function(player)
	if player.Name == "JakyeRU" or player.Name == "Roblox" then
		print(player.Name.. " allowed!")
	else
		print(player.Name.. " is not allowed!")
		player:Kick("Not allowed!")
	end
end)

2 comments

0

Thanks for the help.

0

No problem.

It should be if player == "DavidOfficial_BR" or player == "gustavomfmgame" then , its just the way 'or' works, you can't say X is Y or Z, you must say X is Y or X is Z

1 comment

0

Thanks for explaining it for me.

local allowedPlayers = {204455241,187797090} --I pre-inserted you and your friend's UserIds.

game:GetService("Players").PlayerAdded:Connect(function(Player)
	for i = 1,#allowedPlayers do
		if Player.UserId == allowedPlayers[i] then
			
		else
			Player:Kick("Access denied.")
		end
	end
end)

In the code we have a table of UserIds. Whenever a player joins we loop through the table and check if the player's UserId is in the table. If their UserId is in the table nothing happens. If it isn't then they are immediately kicked from the game.

2 comments

0

"For i" is confusing for me, but i get what the script does. Thanks for helping.

0

I mean, i get the concept.

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