How to ban a player only in one server

Hey! so im making a survival game with my friends and i wanna make it so if u die u get kicked from a server and cant join anymore, so u need to go to another server

And i cant figure out how to ban a player only in one server, Can anyone help me

2 Likes
local Players = game:GetService("Players")

local KickedPlayers = {}

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local Humanoid = Character:WaitForChild("Humanoid")
		Humanoid.Died:Connect(function()
			table.insert(KickedPlayers, Player.UserId)
			Player:Kick("You have died!")
		end)
	end)
	
	if table.find(KickedPlayers, Player.UserId) then
		Player:Kick("You have already died in this server!")
	end
end)

Logic should be relatively simple to follow, I used the “UserId” property of the player instance instead of the player instance itself as the latter would change if the player rejoined the same server instance, the former will never change.

2 Likes

Keep track of all the “banned players” per server in an array like so:

-- Services
local Players = game:GetService("Players")

-- Variables
local banned = {}

-- Functions
local function onPlayerAdded(player: Player)
    if banned[player.UserId] then
        player:Kick("You previously died in this server.")
    end
    
    local function onCharacterAdded(character: Model)
        local humanoid = character:WaitForChild("Humanoid")
        if humanoid then
            humanoid.Died:Connect(function()
                -- kick player & add them to the banned array
                banned[player.UserId] = true
                player:Kick("You died.")
            end)
        end
    end
    if player.Character then
        onCharacterAdded(player.Character)
    end
    player.CharacterAdded:Connect(onCharacterAdded)
end

-- Events
for _, player in pairs(Players:GetPlayers()) do
    onPlayerAdded(player)
end
Players.PlayerAdded:Connect(onPlayerAdded)

Edit: @Forummer made a good point, changed mine to player.UserId as well… :sweat_smile:

3 Likes

Thanks! but is it a local script or a server script?

Thanks! but i couldnt understand (im new still) so could u explain to me?

You can only call “:Kick()” on a player instance from a server script.

1 Like

Put the code under a Server script under ServerScriptService and it should work fine. You can test it by solo playing and resetting your character.

Oh yeah it worked, thanks!

character69420

I have a question lets say I created a ban hammer and when it kicks the player it says who banned you like example:

“you been banned by PlayerName”