Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why the kick system isn't executed?

Asked by 5 years ago
Edited 5 years ago

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?

0
You're doing too many extra steps. Mr_Unlucky 1085 — 5y

3 answers

Log in to vote
0
Answered by
JakyeRU 637 Moderation Voter
5 years ago
Edited 5 years ago
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)
0
Thanks for the help. User#27525 1 — 5y
0
No problem. JakyeRU 637 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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

0
Thanks for explaining it for me. User#27525 1 — 5y
Log in to vote
0
Answered by
Mr_Unlucky 1085 Moderation Voter
5 years ago
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.

0
"For i" is confusing for me, but i get what the script does. Thanks for helping. User#27525 1 — 5y
0
I mean, i get the concept. User#27525 1 — 5y

Answer this question