PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

Kick

The Kick Player method allows a game to gracefully disconnect a client from the game and optionally provide a message to the disconnected player. This is useful for moderating abusive players. When used in conjunction with a DataStore, it is possible to create ban lists with expiration dates. Only allow specific whitelisted users whom you trust to trigger this method on other players.

When used from a LocalScript, only the local player’s client can be kicked.

##Example
When calling this method on a Player with no arguments, they will be disconnected from the server and receive the default message: This game has shut down.

Getting kicked without a message.

Calling this method on a player but providing a string as the first argument will replace this message with the contents of the string.

Getting kicked with a message.

Parameters

Name Type Default Description

The message to show the player upon kicking.

Returns

Return Type Summary

No return.


Code Samples


Kick Command

This code sample allows the creator of a place to kick other players by chatting “/kick xyz”, where xyz is the exact username of a player.

local Players = game:GetService("Players")

local kickCommand = "/kick "

local function onOwnerChatted(player, message)
	if message:sub(1, kickCommand:len()):lower() == kickCommand:lower() then
		local name = message:sub(kickCommand:len() + 1)
		local playerToKick = Players:FindFirstChild(name)
		if playerToKick then
			playerToKick:Kick("You have been kicked by the owner.")
		else
			-- Couldn't find the player in question
		end
	end
end

local function onPlayerAdded(player)
	-- Restrict this command to only the creator/owner
	if player.UserId == game.CreatorId and game.CreatorType == Enum.CreatorType.User then
		player.Chatted:Connect(function (...)
			onOwnerChatted(player, ...)
		end)
	end
end

-- Listen for players being added
for _, player in pairs(Players:GetPlayers()) do
	onPlayerAdded(player)
end
Players.PlayerAdded:Connect(onPlayerAdded)