Help with global ban script

I was scripting my ban system but then i came across this message

and for some reason i don’t know what is causing it

here is the script

-- DataStore

local DataStoreService = game:GetService("DataStoreService")
local PlayerStore = DataStoreService:GetDataStore("PlayerData")

local getSuccess,bannedPlayers = pcall(function()
	return PlayerStore:GetAsync("BannedPlayers")
end)
if not bannedPlayers then
	local bannedPlayers = {}
	local setSuccess,errorMessage = pcall(function()
		PlayerStore:SetAsync("BannedPlayers", bannedPlayers)
	end)
end



---------------------------------------------------------------------------------------------------------------------------------
--- Ban command

local admins = {1593195522, 3505876378}
local prefix = "/" 
local LocalChatMessage = game.ReplicatedStorage.LocalChatMessage

local function checkCommand(player, message)
	message = string.lower(message)
	local args = string.split(message, " ")
	if args[1] == prefix.."ban" then
		print(args[2])
		local id 
		local success, response = pcall(function()
			id = game.Players:GetUserIdFromNameAsync(args[2])
		end) 
		if success then
			table.insert(bannedPlayers,id)
			local setSuccess,errorMessage = pcall(function()
				PlayerStore:SetAsync("BannedPlayers", bannedPlayers)
			end)
			LocalChatMessage:FireClient(player, "Successfully banned "..args[2]..".", true)
		else
			LocalChatMessage:FireClient(player, "Unsuccessfully banned "..args[2]..".", false)
		end
		for _,player in ipairs(game.Players:GetPlayers()) do
			if player.Name:lower() == args[2] then
				player:Kick("get dunked on")
				
				break
			end
		end
	elseif args[1] == prefix.."unban" then
		local id 
		local success, response = pcall(function()
			id = game.Players:GetUserIdFromNameAsync(args[2])
		end) 
		if success then
			table.remove(bannedPlayers, table.find(bannedPlayers, id))
			local setSuccess,errorMessage = pcall(function()
				PlayerStore:SetAsync("BannedPlayers", bannedPlayers)
			end)
			LocalChatMessage:FireClient(player, "Successfully unbanned "..args[2]..".", true)
		else
			LocalChatMessage:FireClient(player, "Unsuccessfully unbanned "..args[2]..".", false)
		end
	end
end

local function checkPlayer(player)
	if table.insert(bannedPlayers, player.UserId) then
		player:Kick("You have been banned for")
		return
	end
	if table.find(admins, player.UserId) then
		player.Chatted:Connect(function(message)
			checkCommand(player, message)
		end)
	end
end
game.Players.PlayerAdded:Connect(checkPlayer)




In one of your lines, you accidently did:

if table.insert(bannedPlayers, player.UserId) then

Instead of:

if table.find(bannedPlayers, player.UserId) then

New code:

-- DataStore

local DataStoreService = game:GetService("DataStoreService")
local PlayerStore = DataStoreService:GetDataStore("PlayerData")

local getSuccess,bannedPlayers = pcall(function()
	return PlayerStore:GetAsync("BannedPlayers")
end)

if not bannedPlayers then
	local bannedPlayers = {}
	
	local setSuccess,errorMessage = pcall(function()
		PlayerStore:SetAsync("BannedPlayers", bannedPlayers)
	end)
end



---------------------------------------------------------------------------------------------------------------------------------
--- Ban command

local admins = {1593195522, 3505876378}
local prefix = "/" 
local LocalChatMessage = game.ReplicatedStorage.LocalChatMessage

local function checkCommand(player, message)
	message = string.lower(message)
	local args = string.split(message, " ")
	if args[1] == prefix.."ban" then
		print(args[2])
		local id = nil
		local success, response = pcall(function()
			id = game.Players:GetUserIdFromNameAsync(args[2])
		end) 
		
		if success then
			table.insert(bannedPlayers,id)
			local setSuccess,errorMessage = pcall(function()
				PlayerStore:SetAsync("BannedPlayers", bannedPlayers)
			end)
			LocalChatMessage:FireClient(player, "Successfully banned "..args[2]..".", true)
		else
			LocalChatMessage:FireClient(player, "Unsuccessfully banned "..args[2]..".", false)
		end
		
		for _,player in ipairs(game.Players:GetPlayers()) do
			if player.Name:lower() == args[2] then
				player:Kick("get dunked on")

				break
			end
		end
	elseif args[1] == prefix.."unban" then
		local id 
		local success, response = pcall(function()
			id = game.Players:GetUserIdFromNameAsync(args[2])
		end) 
		if success then
			table.remove(bannedPlayers, table.find(bannedPlayers, id))
			local setSuccess,errorMessage = pcall(function()
				PlayerStore:SetAsync("BannedPlayers", bannedPlayers)
			end)
			LocalChatMessage:FireClient(player, "Successfully unbanned "..args[2]..".", true)
		else
			LocalChatMessage:FireClient(player, "Unsuccessfully unbanned "..args[2]..".", false)
		end
	end
end

local function checkPlayer(player)
	if table.find(bannedPlayers, player.UserId) then
		player:Kick("You have been banned for")
		
		return
	end
	
	if table.find(admins, player.UserId) then
		player.Chatted:Connect(function(message)
			checkCommand(player, message)
		end)
	end
end

game.Players.PlayerAdded:Connect(checkPlayer)

hmm i tried it and still the same error file:///var/folders/gj/q3y96c7j45g0wp21c20jlptc0000gp/T/TemporaryItems/NSIRD_screencaptureui_fzXwv4/Screen%20Shot%202022-07-29%20at%2011.57.04%20AM.png

Could you send another link of that image?

1 Like

Also I think I have a fix:

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

--//Variables
local PlayerStore = DataStoreService:GetDataStore("PlayerData")
local LocalChatMessage = ReplicatedStorage.LocalChatMessage

--//Controls
local admins = {1593195522, 3505876378}
local prefix = "/" 

--//Initialization
local getSuccess, bannedPlayers = pcall(function()
	return PlayerStore:GetAsync("BannedPlayers")
end)

if not getSuccess and not bannedPlayers then
	bannedPlayers = {}

	local setSuccess, errorMessage = pcall(function()
		PlayerStore:SetAsync("BannedPlayers", bannedPlayers)
	end)
end

--//Functions
local function checkCommand(player, message)
	message = string.lower(message)
	local args = string.split(message, " ")
	
	if args[1] == prefix.."ban" then
		print(args[2])
		local id = nil
		
		local success, response = pcall(function()
			id = Players:GetUserIdFromNameAsync(args[2])
		end) 

		if id then
			table.insert(bannedPlayers,id)
			
			local setSuccess, errorMessage = pcall(function()
				PlayerStore:SetAsync("BannedPlayers", bannedPlayers)
			end)
			
			LocalChatMessage:FireClient(player, "Successfully banned "..args[2]..".", true)
		else
			LocalChatMessage:FireClient(player, "Unsuccessfully banned "..args[2]..".", false)
		end

		for i, player in ipairs(Players:GetPlayers()) do
			if player.Name:lower() == args[2] then
				player:Kick("get dunked on")

				break
			end
		end
	elseif args[1] == prefix.."unban" then
		local id = nil
		
		local success, response = pcall(function()
			id = Players:GetUserIdFromNameAsync(args[2])
		end) 
		
		if id then
			table.remove(bannedPlayers, table.find(bannedPlayers, id))
			
			local setSuccess,errorMessage = pcall(function()
				PlayerStore:SetAsync("BannedPlayers", bannedPlayers)
			end)
			
			LocalChatMessage:FireClient(player, "Successfully unbanned "..args[2]..".", true)
		else
			LocalChatMessage:FireClient(player, "Unsuccessfully unbanned "..args[2]..".", false)
		end
	end
end

local function checkPlayer(player)
	if table.find(bannedPlayers, player.UserId) then
		player:Kick("You have been banned for")

		return
	end

	if table.find(admins, player.UserId) then
		player.Chatted:Connect(function(message)
			checkCommand(player, message)
		end)
	end
end

Players.PlayerAdded:Connect(function(player)
	checkPlayer(player)
end)
1 Like


same thing

Can you add a print(bannedPlayers) somewhere in the script and see what it prints?

K so, i tried it and the value came out to nil

Try this:

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

--//Variables
local PlayerStore = DataStoreService:GetDataStore("PlayerData")
local LocalChatMessage = ReplicatedStorage.LocalChatMessage

--//Controls
local admins = {1593195522, 3505876378}
local prefix = "/" 
local bannedPlayers = {}

--//Initialization
local getSuccess, errorMessage = pcall(function()
	bannedPlayers = PlayerStore:GetAsync("BannedPlayers")
end)

--//Functions
local function checkCommand(player, message)
	message = string.lower(message)
	local args = string.split(message, " ")
	
	if args[1] == prefix.."ban" then
		print(args[2])
		local id = nil
		
		local success, response = pcall(function()
			id = Players:GetUserIdFromNameAsync(args[2])
		end) 

		if id then
			table.insert(bannedPlayers,id)
			
			local setSuccess, errorMessage = pcall(function()
				PlayerStore:SetAsync("BannedPlayers", bannedPlayers)
			end)
			
			LocalChatMessage:FireClient(player, "Successfully banned "..args[2]..".", true)
		else
			LocalChatMessage:FireClient(player, "Unsuccessfully banned "..args[2]..".", false)
		end

		for i, player in ipairs(Players:GetPlayers()) do
			if player.Name:lower() == args[2] then
				player:Kick("get dunked on")

				break
			end
		end
	elseif args[1] == prefix.."unban" then
		local id = nil
		
		local success, response = pcall(function()
			id = Players:GetUserIdFromNameAsync(args[2])
		end) 
		
		if id then
			table.remove(bannedPlayers, table.find(bannedPlayers, id))
			
			local setSuccess,errorMessage = pcall(function()
				PlayerStore:SetAsync("BannedPlayers", bannedPlayers)
			end)
			
			LocalChatMessage:FireClient(player, "Successfully unbanned "..args[2]..".", true)
		else
			LocalChatMessage:FireClient(player, "Unsuccessfully unbanned "..args[2]..".", false)
		end
	end
end

local function checkPlayer(player)
	if table.find(bannedPlayers, player.UserId) then
		player:Kick("You have been banned for")

		return
	end

	if table.find(admins, player.UserId) then
		player.Chatted:Connect(function(message)
			checkCommand(player, message)
		end)
	end
end

Players.PlayerAdded:Connect(function(player)
	checkPlayer(player)
end)

same thing

try this

-- DataStore

local DataStoreService = game:GetService("DataStoreService")
local PlayerStore = DataStoreService:GetDataStore("PlayerData")

local getSuccess,bannedPlayers = pcall(function()
	return PlayerStore:GetAsync("BannedPlayers")
end)
if not bannedPlayers then
	bannedPlayers = {}
	local setSuccess,errorMessage = pcall(function()
		PlayerStore:SetAsync("BannedPlayers", bannedPlayers)
	end)
end



---------------------------------------------------------------------------------------------------------------------------------
--- Ban command

local admins = {1593195522, 3505876378}
local prefix = "/" 
local LocalChatMessage = game.ReplicatedStorage.LocalChatMessage

local function checkCommand(player, message)
	message = string.lower(message)
	local args = string.split(message, " ")
	if args[1] == prefix.."ban" then
		print(args[2])
		local id 
		local success, response = pcall(function()
			id = game.Players:GetUserIdFromNameAsync(args[2])
		end) 
		if success then
			table.insert(bannedPlayers,id)
			local setSuccess,errorMessage = pcall(function()
				PlayerStore:SetAsync("BannedPlayers", bannedPlayers)
			end)
			LocalChatMessage:FireClient(player, "Successfully banned "..args[2]..".", true)
		else
			LocalChatMessage:FireClient(player, "Unsuccessfully banned "..args[2]..".", false)
		end
		for _,player in ipairs(game.Players:GetPlayers()) do
			if player.Name:lower() == args[2] then
				player:Kick("get dunked on")
				
				break
			end
		end
	elseif args[1] == prefix.."unban" then
		local id 
		local success, response = pcall(function()
			id = game.Players:GetUserIdFromNameAsync(args[2])
		end) 
		if success then
			table.remove(bannedPlayers, table.find(bannedPlayers, id))
			local setSuccess,errorMessage = pcall(function()
				PlayerStore:SetAsync("BannedPlayers", bannedPlayers)
			end)
			LocalChatMessage:FireClient(player, "Successfully unbanned "..args[2]..".", true)
		else
			LocalChatMessage:FireClient(player, "Unsuccessfully unbanned "..args[2]..".", false)
		end
	end
end

local function checkPlayer(player)
	if table.find(bannedPlayers, player.UserId) then
		player:Kick("You have been banned for")
		return
	end
	if table.find(admins, player.UserId) then
		player.Chatted:Connect(function(message)
			checkCommand(player, message)
		end)
	end
end
game.Players.PlayerAdded:Connect(checkPlayer)

Oh my thank you so much! I realised why its not working now. Thank you!

it was here
you were saying

local bannedPlayers = {}

which creates a new variable, and i fixed something else, the table.insert and table.find, it was pointed out earlier tho.

o thanks a lot though dude i really appreciate it

1 Like

Hey so I just was trying to use the command again and it didnt work

1 Like

I don’t know why… I tried some stuff

Hey, i was asleep, dm me Msix29#7740, ill help you, i might make you a whole new admin commands but your choice

Also, you can do this:

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

--//Variables
local PlayerStore = DataStoreService:GetDataStore("PlayerData")
local LocalChatMessage = ReplicatedStorage.LocalChatMessage

--//Controls
local admins = {"L6ker", "Peapod_iDTech"}
local prefix = "/" 

--//Initialization
local getSuccess, bannedPlayers = pcall(function()
	return PlayerStore:GetAsync("BannedPlayers")
end)

if not getSuccess and not bannedPlayers then
	bannedPlayers = {}

	local setSuccess, errorMessage = pcall(function()
		PlayerStore:SetAsync("BannedPlayers", bannedPlayers)
	end)
end

--//Functions
local function checkCommand(player, message)
	message = string.lower(message)
	local args = string.split(message, " ")
	
	if args[1] == prefix.."ban" then
		print(args[2])
		local id = nil
		
		local success, response = pcall(function()
			id = Players:GetUserIdFromNameAsync(args[2])
		end) 

		if id then
			table.insert(bannedPlayers,id)
			
			local setSuccess, errorMessage = pcall(function()
				PlayerStore:SetAsync("BannedPlayers", bannedPlayers)
			end)
			
			LocalChatMessage:FireClient(player, "Successfully banned "..args[2]..".", true)
		else
			LocalChatMessage:FireClient(player, "Unsuccessfully banned "..args[2]..".", false)
		end

		for i, player in ipairs(Players:GetPlayers()) do
			if player.Name:lower() == args[2] then
				player:Kick("get dunked on")

				break
			end
		end
	elseif args[1] == prefix.."unban" then
		local id = nil
		
		local success, response = pcall(function()
			id = Players:GetUserIdFromNameAsync(args[2])
		end) 
		
		if id then
			table.remove(bannedPlayers, table.find(bannedPlayers, id))
			
			local setSuccess,errorMessage = pcall(function()
				PlayerStore:SetAsync("BannedPlayers", bannedPlayers)
			end)
			
			LocalChatMessage:FireClient(player, "Successfully unbanned "..args[2]..".", true)
		else
			LocalChatMessage:FireClient(player, "Unsuccessfully unbanned "..args[2]..".", false)
		end
	end
end

local function checkPlayer(player)
	if table.find(bannedPlayers, player.UserId) then
		player:Kick("You have been banned for")

		return
	end

	if table.find(admins, player.UserId) then
		player.Chatted:Connect(function(message)
			checkCommand(player, message)
		end)
	end
end

Players.PlayerAdded:Connect(function(player)
	checkPlayer(player)
end)

It’s for the usernames…