Why This Ban Command Doesn't Work?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make a ban and unban system.
  2. What is the issue? Include screenshots / videos if possible!
    it kicks me from the server when i ban myself and kicks for the reason also but when i rejoin it doesn’t kicks me. ( No Errors and I Am Trying On Roblox Studio And I Don’t Think That’s The Problem )
local admins = game:GetService('ServerStorage').Admins

local DSS = game:GetService('DataStoreService')

local playerData = DSS:GetDataStore('playerData')

local function setBan(player, reason)
	local success, errorMessage = pcall(function()
		playerData:SetAsync("bans/" .. tostring(player.UserId), {Ban = true, BanReason = reason})
	end)
	if not success then
		warn(errorMessage)
	else
		game:GetService('Players'):FindFirstChild(player):Kick(reason)
	end
end

local function setUnBan(player)
	local success, errorMessage = pcall(function()
		playerData:SetAsync("bans/" .. tostring(player.UserId), {Ban = false})
	end)
	if not success then
		warn(errorMessage)
	end
end

local function getBan(player)
	local data
	local sucess, errorMessage = pcall(function()
		data = playerData:GetAsync("bans/" .. tostring(player.UserId))
	end)
	if sucess and data then
		if data.Ban == true then
			return true, data.BanReason
		else
			return false
		end
	end
end

game:GetService('ReplicatedStorage').RemoteEvents.Handler.OnServerEvent:Connect(function(player, command, plr, reason)
	if admins:FindFirstChild(player.UserId) ~= nil then
		if string.lower(command) == 'ban' then
			if game:GetService('Players'):FindFirstChild(plr) then
				setBan(plr, reason)
			end
		elseif string.lower(command) == 'unban' then
			setUnBan(plr)
		end
	end
end)

game.Players.PlayerAdded:Connect(function(player)
	local banned = getBan(player)
	
	if banned == true then
		player:Kick(banned[1])
	end
end)

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I Tried Looking On If Even It Saves On DataStore In Data Editor v2 ( Free Version ) but it just shows nil ( default value ).

I’m fairly sure you’re indexing banned = getBan wrong. You should only be returning the BanReason.

Code:

--//Services
local Players = game:GetService("Players")
local DSS = game:GetService('DataStoreService')
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService('ReplicatedStorage')

--//Variables
local admins = ServerStorage.Admins
local playerData = DSS:GetDataStore('playerData')

--//Functions
local function setBan(player, reason)
	local success, errorMessage = pcall(function()
		playerData:SetAsync("bans/" .. tostring(player.UserId), {Ban = true, BanReason = reason})
	end)
	
	if not success then
		warn(errorMessage)
	else
		Players:FindFirstChild(player):Kick(reason)
	end
end

local function removeBan(player)
	local success, errorMessage = pcall(function()
		playerData:SetAsync("bans/" .. tostring(player.UserId), {Ban = false})
	end)
	
	if not success then
		warn(errorMessage)
	end
end

local function getBan(player)
	local data = nil
	
	local sucess, errorMessage = pcall(function()
		data = playerData:GetAsync("bans/" .. tostring(player.UserId))
	end)
	
	if not sucess then
		warn(errorMessage)
		return "Unable to load ban data"
	end
	
	if data and data.Ban then
		return data.BanReason
	else
		return false
	end
end

ReplicatedStorage.RemoteEvents.Handler.OnServerEvent:Connect(function(player, command, plr, reason)
	if admins:FindFirstChild(player.UserId) then
		if string.lower(command) == 'ban' then
			if Players:FindFirstChild(plr) then
				setBan(plr, reason)
			end
		elseif string.lower(command) == 'unban' then
			removeBan(plr)
		end
	end
end)

Players.PlayerAdded:Connect(function(player)
	local banReason = getBan(player)

	if banReason then
		player:Kick(banReason)
	end
end)

Also, I’m not sure what value plr is. Is that a name or an actual player? If you could show your client code that would help too.

client code :

script.Parent.MouseButton1Click:Connect(function()
	game:GetService('ReplicatedStorage').RemoteEvents.Handler:FireServer('Ban', script.Parent.Parent.Person.Text, script.Parent.Parent.Reason.Text)
end)

Did my script work? If not, switch the server script to this:

--//Services
local Players = game:GetService("Players")
local DSS = game:GetService('DataStoreService')
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService('ReplicatedStorage')

--//Variables
local admins = ServerStorage.Admins
local playerData = DSS:GetDataStore('playerData')

--//Functions
local function setBan(player, reason)
	local success, errorMessage = pcall(function()
		playerData:SetAsync("bans/" .. tostring(player.UserId), {Ban = true, BanReason = reason})
	end)

	if not success then
		warn(errorMessage)
	else
		Players:FindFirstChild(player):Kick(reason)
	end
end

local function removeBan(playerName)
	local userId = Players:GetUserIdFromNameAsync(playerName)
	
	if not userId then
		return
	end
	
	local success, errorMessage = pcall(function()
		playerData:SetAsync("bans/" .. userId, {Ban = false})
	end)

	if not success then
		warn(errorMessage)
	end
end

local function getBan(player)
	local data = nil

	local sucess, errorMessage = pcall(function()
		data = playerData:GetAsync("bans/" .. tostring(player.UserId))
	end)

	if not sucess then
		warn(errorMessage)
		return "Unable to load ban data"
	end

	if data and data.Ban then
		return data.BanReason
	else
		return false
	end
end

ReplicatedStorage.RemoteEvents.Handler.OnServerEvent:Connect(function(player, command, playerName, reason)
	if admins:FindFirstChild(player.UserId) then
		if string.lower(command) == 'ban' then
			local commandVictim = Players:FindFirstChild(playerName)
			if commandVictim then
				setBan(commandVictim, reason)
			end
		elseif string.lower(command) == 'unban' then
			removeBan(playerName)
		end
	end
end)

Players.PlayerAdded:Connect(function(player)
	local banReason = getBan(player)

	if banReason then
		player:Kick(banReason)
	end
end)

It’s because you didn’t save the data correctly.

I modified a little bit your code and still doesn’t works.

local admins = game:GetService('ServerStorage').Admins

local DSS = game:GetService('DataStoreService')

local playerData = DSS:GetDataStore('playerData')

local function setBan(player, reason)
	local UserId = game:GetService('Players'):GetUserIdFromNameAsync(player)
	
	local success, errorMessage = pcall(function()
		playerData:SetAsync("bans/" .. tostring(UserId), {Ban = true, BanReason = reason})
	end)
	if not success then
		warn(errorMessage)
	else
		game:GetService('Players'):FindFirstChild(player):Kick(reason)
	end
end

local function setUnBan(player)
	local UserId = game:GetService('Players'):GetUserIdFromNameAsync(player)
	
	local success, errorMessage = pcall(function()
		playerData:SetAsync("bans/" .. tostring(UserId), {Ban = false})
	end)
	if not success then
		warn(errorMessage)
	end
end

local function getBan(player)
	local UserId = game:GetService('Players'):GetUserIdFromNameAsync(player)
	
	local data
	local sucess, errorMessage = pcall(function()
		data = playerData:GetAsync("bans/" .. tostring(UserId))
	end)
	if data and data.Ban then
		return data.BanReason
	else
		return false
	end
end

game:GetService('ReplicatedStorage').RemoteEvents.Handler.OnServerEvent:Connect(function(player, command, plr, reason)
	if admins:FindFirstChild(player.UserId) ~= nil then
		if string.lower(command) == 'ban' then
			if game:GetService('Players'):FindFirstChild(plr) then
				setBan(plr, reason)
			end
		elseif string.lower(command) == 'unban' then
			setUnBan(plr)
		end
	end
end)

game.Players.PlayerAdded:Connect(function(player)
	local banned = getBan(player)
	
	if banned then
		game:GetService('Players'):FindFirstChild(player):Kick(banned)
	end
end)

It saves to the datastore because it printed (‘datastore added to queue’) after i accidently clicked twice to ban but it doesn’t kicks or it doesn’t get the player data.

I made an error, try this code:

--//Services
local Players = game:GetService("Players")
local DSS = game:GetService('DataStoreService')
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService('ReplicatedStorage')

--//Variables
local admins = ServerStorage.Admins
local playerData = DSS:GetDataStore('playerData')

--//Functions
local function setBan(player, reason)
	local success, errorMessage = pcall(function()
		playerData:SetAsync("bans/" .. tostring(player.UserId), {Ban = true, BanReason = reason})
	end)

	if not success then
		warn(errorMessage)
	else
		player:Kick(reason)
	end
end

local function removeBan(playerName)
	local userId = Players:GetUserIdFromNameAsync(playerName)
	
	if not userId then
		return
	end
	
	local success, errorMessage = pcall(function()
		playerData:SetAsync("bans/" .. userId, {Ban = false})
	end)

	if not success then
		warn(errorMessage)
	end
end

local function getBan(player)
	local data = nil

	local sucess, errorMessage = pcall(function()
		data = playerData:GetAsync("bans/" .. tostring(player.UserId))
	end)

	if not sucess then
		warn(errorMessage)
		return "Unable to load ban data"
	end

	if data and data.Ban then
		return data.BanReason
	else
		return false
	end
end

ReplicatedStorage.RemoteEvents.Handler.OnServerEvent:Connect(function(player, command, playerName, reason)
	if admins:FindFirstChild(player.UserId) then
		if string.lower(command) == 'ban' then
			local commandVictim = Players:FindFirstChild(playerName)
			if commandVictim then
				setBan(commandVictim, reason)
			end
		elseif string.lower(command) == 'unban' then
			removeBan(playerName)
		end
	end
end)

Players.PlayerAdded:Connect(function(player)
	local banReason = getBan(player)

	if banReason then
		player:Kick(banReason)
	end
end)

Also, please tell me if there are any errors. Also, .Text is supposed to be the player’s name right?

  1. Yes Person.Text is Player and Reason.Text is Reason But They Works.
  2. it didn’t work again.
local admins = game:GetService('ServerStorage').Admins

local DSS = game:GetService('DataStoreService')

local playerData = DSS:GetDataStore('playerData')

local function setBan(player, reason)
	local UserId = game:GetService('Players'):GetUserIdFromNameAsync(player)
	
	local success, errorMessage = pcall(function()
		playerData:SetAsync("bans/" .. tostring(UserId), {Ban = true, BanReason = reason})
	end)
	if not success then
		warn(errorMessage)
	else
		game:GetService('Players'):FindFirstChild(player):Kick(reason)
	end
end

local function setUnBan(player)
	local UserId = game:GetService('Players'):GetUserIdFromNameAsync(player)
	
	local success, errorMessage = pcall(function()
		playerData:SetAsync("bans/" .. tostring(UserId), {Ban = false})
	end)
	if not success then
		warn(errorMessage)
	end
end

local function getBan(player)
	local UserId = game:GetService('Players'):GetUserIdFromNameAsync(player)
	
	local data
	local sucess, errorMessage = pcall(function()
		data = playerData:GetAsync("bans/" .. tostring(UserId))
	end)
	
	if not sucess then
		warn(errorMessage)
		return "Unable to load ban data"
	end
	
	if data and data.Ban then
		return data.BanReason
	else
		return false
	end
end

game:GetService('ReplicatedStorage').RemoteEvents.Handler.OnServerEvent:Connect(function(player, command, plr, reason)
	if admins:FindFirstChild(player.UserId) ~= nil then
		if string.lower(command) == 'ban' then
			if game:GetService('Players'):FindFirstChild(plr) then
				setBan(plr, reason)
			end
		elseif string.lower(command) == 'unban' then
			setUnBan(plr)
		end
	end
end)

game.Players.PlayerAdded:Connect(function(player)
	local banned = getBan(player)
	
	if banned then
		game:GetService('Players'):FindFirstChild(player):Kick(banned)
	end
end)

I don’t think you’re using the right script. And like I said, are there any errors?

nope not any errors or warnings or something.

most confusing part is there is no errors

I figured out how i don’t know but i think the problem is getban because i tried else print(‘no’) and it printed no

you pass the player to the function

Here you will get the wrong ID, use local UserId = game:GetService('Players'):GetUserIdFromNameAsync(player.Name)

local UserId = game:GetService(‘Players’):GetUserIdFromNameAsync(player.Name)

argument 1 missing or nil

I think these are the reason when returning values you didnt return an array, instead of

local banned = getBan(player)

it should’ve been

local banned, reason = getBan(player)
player:Kick(reason)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.