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?
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)
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)
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
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.