Why does my ban command only kick the player once but they can rejoin just fine

I am currently making an admin command script but I have a ban player command I found but it only kicks out the player once but the player can just rejoin the game and nothing will happen here is the code:

local DataStoreService = game:GetService("DataStoreService")
local BannedPlayersData = DataStoreService:GetDataStore('BannedPlayers')
local Players = game:GetService("Players")

local Trigger = '/Ban '

local AD = {'PuffyJasonrocks84'}
local Banned = {}

Players.PlayerAdded:Connect(function(player)
	local Data = BannedPlayersData:GetAsync('BannedPlayers', Banned) or {}
	Banned = Data
	
	for _,v in pairs(Banned) do
		if player.Name == v then
			player:Kick("You Have Been Banned By An Admin")
		end
	end
	
	player.Chatted:Connect(function(Chat)
		local Check = string.sub(Chat, 1, 5)
		if string.match(Trigger, Check) then
			local FIND = string.sub(Chat, 6, #Chat)
			if Players:FindFirstChild(FIND) then
				for _,v in pairs(AD) do
					if player.Name == v then
						Players:FindFirstChild(FIND):Kick('You Have Been Banned by An Admin!')
					end
				end
			end
		end
	end)
end)
Players.PlayerRemoving:Connect(function()	
	BannedPlayersData:SetAsync('BannedPlayers', Banned)
end)

Thanks.

1 Like

I haven’t worked with datastores in a hot minute but I believe I remember a few things.
One, have you tested it in a real game? I forgot where its stated but sometimes APIs like DataStoreService dont work in studio and such.

BannedPlayersData:GetAsync('BannedPlayers', Banned) or {}
GetAsync() has one argument, a key value. That banned table that you are passing in is redundant because it will be discarded. That might not be the cause of your error but I still recommend taking it out.

Additionally, I don’t see you insert any sort of ID or identifier inside of the banned table so nothing is really updating. Hopefully you can point out where it is but otherwise I dont see it. If anything, I recommend doing a table.insert on the banned table with the player’s name since that is what you check.

2 Likes

How would I do this? (I’m not good at using tables)

Use datastore then

local DatastoreSevice = game:GetService("DataStoreService")
local IsBanned = DatastoreSevice:GetGlobalDataStore("Banned")

game.Players.PlayerAdded:Connect(function(Player)
	print(IsBanned:GetAsync(Player.UserId))
	
	if IsBanned:GetAsync(Player.UserId) == true then
		print("Gaming")
	end
	IsBanned:SetAsync(Player.UserId, true) -- for testing
end)

wait nvm you already have that in the code i think

1 Like

but heres how you ban them if you want to

IsBanned:SetAsync(Player.UserId, IsBanned)
1 Like

This code should work. I tested it by banning myself lol

local DataStoreService = game:GetService("DataStoreService")
local BannedPlayersData = DataStoreService:GetDataStore('BannedPlayers')
local Players = game:GetService("Players")

local Trigger = '/Ban '

local AD = {'PuffyJasonrocks84'}
local Banned = {}

Players.PlayerAdded:Connect(function(player)
	local PlayerId = player.UserId
	
	if Banned[PlayerId] then
		player:Kick("You are banned")
	end
	
	local Data
	local success, errormessage = pcall(function()
		Data = BannedPlayersData:GetAsync(PlayerId)
	end)
	
	if Data == true then
		player:Kick("You are banned")
	end
	
	player.Chatted:Connect(function(Chat)
		local Check = string.sub(Chat, 1, 5)
		if string.match(Trigger, Check) then
			local FIND = string.sub(Chat, 6, #Chat)
			if Players:FindFirstChild(FIND) then
				for _,v in pairs(AD) do
					if player.Name == v then
						local bannedplayer = Players:FindFirstChild(FIND)
						if bannedplayer then
							local bannedplayerUserId = bannedplayer.UserId
							BannedPlayersData:SetAsync(bannedplayerUserId, true)
							task.wait(1)
							Players:FindFirstChild(FIND):Kick('You Have Been Banned by An Admin!')
						end
					end
				end
			end
		end
	end)
end)
1 Like

ok it fully works now, and i cant join my game

local DatastoreSevice = game:GetService("DataStoreService")
local IsBanned = DatastoreSevice:GetGlobalDataStore("Banned")

game.Players.PlayerAdded:Connect(function(Player)
	if IsBanned:GetAsync(Player.UserId) == true then
		Player:Kick("No rejoining then")
	end
	IsBanned:SetAsync(Player.UserId, true) -- for testing
end)

And your script combined to mine

local DatastoreSevice = game:GetService("DataStoreService")
local IsBanned = DatastoreSevice:GetGlobalDataStore("Banned")

local Players = game:GetService("Players")

local Trigger = '/Ban '

local AD = {'PuffyJasonrocks84'}

Players.PlayerAdded:Connect(function(player)
	print(IsBanned:GetAsync(player.UserId))

	if IsBanned:GetAsync(player.UserId) == true then
		player:Kick("You Have Been Banned by An Admin!")
	end
	
	player.Chatted:Connect(function(Chat)
		local Check = string.sub(Chat, 1, 5)
		if string.match(Trigger, Check) then
			local FIND = string.sub(Chat, 6, #Chat)
			if Players:FindFirstChild(FIND) then
				for _,v in pairs(AD) do
					if player.Name == v then
						Players:FindFirstChild(FIND):Kick('You Have Been Banned by An Admin!')
						
						IsBanned:SetAsync(Players.UserId, true)
					end
				end
			end
		end
	end)
end)
1 Like