Ban/Unban script

Hi, I’m a beginner developer learning how to script admin commands.

  1. What do I want to achieve?
    A ban/Unban script.
  2. What is the issue?
    The unbanning part doesn’t work.

I’ve tried many different solutions out there, none of them work for my script.

local datastore = game:GetService("DataStoreService")
local BanStore = datastore:GetDataStore("banstore111111")
local banCommand = "$ban ([%w_]+)"
local unbanCommand = "$unban ([%w_]+)"
local admins = {"81Frames"}

game.Players.PlayerAdded:Connect(function(plr)

	local IsBanned = BanStore:GetAsync(plr.UserId.."-Banned") or false

	if IsBanned then
		plr:Kick("[Blox by itli] You have been banned.")
	end

	local executorName = "Unknown"  -- Default value for the executor's name

	plr.Chatted:Connect(function(msg)
		local isAdmin = false
		if isAdmin or table.find(admins, plr.Name) then
			-- Ban command
			local bannedPlrName = msg:match(banCommand)
			if bannedPlrName then
				executorName = plr.Name -- Set the executor's name
				for i, p in pairs(game.Players:GetPlayers()) do
					if string.lower(p.Name) == string.lower(bannedPlrName) or string.lower(p.DisplayName) == string.lower(bannedPlrName) then
						local success, err = pcall(function()
							BanStore:SetAsync(p.UserId.. "-Banned", true)
							p:Kick("You have been banned by ".. executorName ..".")
							warn("COMMAND EXECUTED: "..executorName.." has banned "..bannedPlrName)
						end)
						if err then
							warn(err)
						end
						break
					end
				end
			end

			-- Unban command
			local unbannedPlrName = msg:match(unbanCommand)
			if unbannedPlrName then
				executorName = plr.Name -- Set the executor's name
				local success, err = pcall(function()
					if game.Players:FindFirstChild(unbannedPlrName) then
						BanStore:RemoveAsync(game.Players:GetUserIdFromNameAsync())
						warn("COMMAND EXECUTED: " .. executorName .. " has unbanned " .. unbannedPlrName)
					else
						warn("WARNING: Player not found to be unbanned: " .. unbannedPlrName)
					end
				end)
				if err then
					warn(err)
				end
			end
		end
	end)
end)

Thanks.

7 Likes

Hi there. When you say the unbanning part doesn’t work, do you mean it fails to remove the player from the banlist?

4 Likes

It doesn’t unban the player that is banned.

4 Likes

Is there an error message from warn("WARNING: Player not found to be unbanned: " .. unbannedPlrName)?

What’s the message? @81Frames

3 Likes

When I try to unban, It does come up with that message.

4 Likes

(Ignoring the entire script)

You’re basically giving void to GetUserIdFromNameAsync, like there’s no argument here. You should do:

local PlayerId = game.Players:GetUserIdFromNameAsync(unbannedPlrName)
BanStore:SetAsync(PlayerId, false)

And uhh, rewriting the pcall, it could be:

local suc, Player = pcall(function()
return game.Players:FindFirstChild(unbannedPlrName) 
end)
if Player == nil then
warn("Players doesnt exists!")
else
local PlayerId = game.Players:GetUserIdFromNameAsync(unbannedPlrName)
BanStore:SetAsync(PlayerId, false)
end
4 Likes

What’s the message from warn(err)?

3 Likes

Basically says it can’t find the player

3 Likes

And also, I forgot to say, it will return If the player is on the server, not really if the player is a valid player. I recommend you to check if Player exists in Roblox as this way

local suc, Player = pcall(function()
return game.Players:GetUserIdFromNameAsync(unbannedPlrName)
end)
if Player == nil then
-- It return nil as default when this is not a valid PlayerId.
warn("Players doesnt exists!")
else
BanStore:SetAsync(Player , false)
end
4 Likes

Sorry for this reply,
I’m having trouble with the script. I’m getting an unknown global??

local unbannedPlrName = msg:match(unbanCommand)
if unbannedPlrName then
executorName = plr.Name – Set the executor’s name
local suc, Player = pcall(function()
return game.Players:GetUserIdFromNameAsync(unbannedPlrName)
end)
if Player == nil then
– It return nil as default when this is not a valid PlayerId.
warn(“Players doesnt exists!”)
else
BanStore:SetAsync(Player , false)
end

3 Likes

At what line you got the unknown global?

3 Likes

local unbannedPlrName = msg:match(unbanCommand)

3 Likes

Can you take a screenshot of the output?, It’s weird, because its seems to be good. I mean, It’s the same index used to the bannedPlrName and If ban command work, this should work…

3 Likes

3 Likes

Oh, this make sense. msg is unknown global, because msg is an argument of Chatted event and it’s outside of that function. Paste the script and I’ll fix it

3 Likes

Thanks alot.

local unbannedPlrName = msg:match(unbanCommand)
	if unbannedPlrName then
		executorName = plr.Name -- Set the executor's name
		local success, Player = pcall(function()
			local playerId = game.Players:GetUserIdFromNameAsync(unbannedPlrName)
			return playerId
		end)
		if success then
			if Player == nil then
				warn("Player doesn't exist!")
			else
				BanStore:SetAsync(Player, false)
				warn("[Blox by itli] COMMAND EXECUTED: " .. executorName .. " has unbanned " .. unbannedPlrName)
			end
		else
			warn(Player) -- `Player` will contain the error message in case there was an issue with `GetUserIdFromNameAsync`
		end
	end
	end)
2 Likes

The entire script I mean. Because it will be hard to try to explain

3 Likes

Oh, sorry about that.

local datastore = game:GetService("DataStoreService")
local BanStore = datastore:GetDataStore("banstore111111")
local banCommand = "$ban ([%w_]+)"
local unbanCommand = "$unban ([%w_]+)"
local admins = {"81Frames"}

game.Players.PlayerAdded:Connect(function(plr)

	local IsBanned = BanStore:GetAsync(plr.UserId.."-Banned") or false

	if IsBanned then
		plr:Kick("[Blox by itli] You have been banned.")
	end

	local executorName = "Unknown"  -- Default value for the executor's name

	plr.Chatted:Connect(function(msg)
		local isAdmin = false
		if isAdmin or table.find(admins, plr.Name) then
			local bannedPlrName = msg:match(banCommand)
			if bannedPlrName then
				executorName = plr.Name
				for i, p in pairs(game.Players:GetPlayers()) do
					if string.lower(p.Name) == string.lower(bannedPlrName) or string.lower(p.DisplayName) == string.lower(bannedPlrName) then
						local success, err = pcall(function()
							BanStore:SetAsync(p.UserId.. "-Banned", true)
							p:Kick("You have been banned by ".. executorName ..".")
							warn("COMMAND EXECUTED: "..executorName.." has banned "..bannedPlrName)
						end)
						if err then
							warn(err)
						end
						break
					end
				end
			end
		end
	end)
		
					
			
			

			-- Unban command
	local unbannedPlrName = msg:match(unbanCommand)
	if unbannedPlrName then
		executorName = plr.Name -- Set the executor's name
		local success, Player = pcall(function()
			local playerId = game.Players:GetUserIdFromNameAsync(unbannedPlrName)
			return playerId
		end)
		if success then
			if Player == nil then
				warn("Player doesn't exist!")
			else
				BanStore:SetAsync(Player, false)
				warn("[Blox by itli] COMMAND EXECUTED: " .. executorName .. " has unbanned " .. unbannedPlrName)
			end
		else
			warn(Player) -- `Player` will contain the error message in case there was an issue with `GetUserIdFromNameAsync`
		end
	end
	end)
3 Likes

Assuming you only want to ban a player that is on the server, then use this

local datastore = game:GetService("DataStoreService")
local BanStore = datastore:GetDataStore("banstore111111")
local banCommand = "$ban ([%w_]+)"
local unbanCommand = "$unban ([%w_]+)"
local admins = {"81Frames"}

game.Players.PlayerAdded:Connect(function(plr)

	local IsBanned = BanStore:GetAsync(plr.UserId.."-Banned") or false

	if IsBanned then
		plr:Kick("[Blox by itli] You have been banned.")
	end

plr.Chatted:Connect(function(msg)
	if table.find(admins, plr.Name) then --- If the player is an admin.
        if msg:match(banCommand) then
		local bannedPlrName = msg:match(banCommand)
        local PlayerInCommand = game.Players:FindFirstChild(bannedPlrName)
		      if PlayerInCommand then -- If the player is on the server.
		      BanStore:SetAsync(PlayerInCommand.UserId.. "-Banned", true)
		     PlayerInCommand:Kick("You have been banned by ".. plr.Name ..".")
		      warn("COMMAND EXECUTED: "..plr.Name.." has banned ".. PLayerInCommand.Name)
else
             warn("Player is not in the server!")
		end
elseif msg:match(unbanCommand) then
local unbannedPlrName = msg:match(banCommand)
local success, PlayerId = pcall(function()
			return game.Players:GetUserIdFromNameAsync(unbannedPlrName)
end)
if PlayerId == nil then
arn("This is not a valid player.")
else
BanStore:SetAsync(PlayerId, false)
warn("[Blox by itli] COMMAND EXECUTED: " .. plr.Name .. " has unbanned " .. unbannedPlrName)
end
end
end
end)
end)
2 Likes

I see it, forgot to change unbannedPlrName.

Change it to local unbannedPlrName = msg:match(unbanCommand)

3 Likes