Ban Panel Error

Right, so I’ve been attempting to make a Ban Panel for the last few days. I finally found a tutorial which gave me something that should work. So, basically I got a error for the timed ban part.

Error

14:18:57.849 ServerScriptService.AdminChecker.Admin.panel.BanHandler:9: attempt to perform arithmetic (mul) on nil and number - Server - BanHandler:9

Anyways heres the Ban Handler Script.

Code
local config = require(script.Parent.Config)
local datastoreservice = game:GetService("DataStoreService") -- These two lines are the datastores
local bandata = datastoreservice:GetDataStore(config.DataStoreKey)

script.Parent.Ban.OnServerEvent:Connect(function(plr,plrtoban,reason,days,status) -- When you fire the server
	local uid = game.Players:GetUserIdFromNameAsync(plrtoban) -- If they aren't in the server, it gets the id anyway with no error unless it's an invalid name
	local ostime = os.time() -- Time
	local days = tonumber(days) -- Because we used a string, we need to convert it into a number for the multiplication to work
	local d = days * 86400 -- Multiplies by how many seconds are in a day
	local length = d + ostime -- Lengh of the ban
	local table2 = {uid,length,reason} -- I made a table here but you could of done bandata:SetAsync(uid,{uid,length,reason})
	bandata:SetAsync(uid,table2) -- Store the data
	status.Text = "Banned: "..plrtoban..""
	if game.Players:FindFirstChild(plrtoban) == nil then
		print("Not in server")
	else
		game.Players:FindFirstChild(plrtoban):Kick("You have been banned\n For: "..reason.."") -- Kick if they are in the server
	end
end)

If anyone could help me that would be amazing as I have been trying to make one for days and I may finally be at the end point.

For some reason this line is evaluating as nil, try printing the value of days and send it here.

Itprints

  14:28:44.417   Ban Length (days)  -  Server - BanHandler:7

For an added layer of protection, wrap an if statement around the rest of your code:

script.Parent.Ban.OnServerEvent:Connect(function(plr,plrtoban,reason,days,status) -- When you fire the server
	local uid = game.Players:GetUserIdFromNameAsync(plrtoban) -- If they aren't in the server, it gets the id anyway with no error unless it's an invalid name
	local ostime = os.time() -- Time
	local days = tonumber(days) -- Because we used a string, we need to convert it into a number for the multiplication to work
	if days then
		local d = days * 86400 -- Multiplies by how many seconds are in a day
		local length = d + ostime -- Lengh of the ban
		local table2 = {uid,length,reason} -- I made a table here but you could of done bandata:SetAsync(uid,{uid,length,reason})
		bandata:SetAsync(uid,table2) -- Store the data
		status.Text = "Banned: "..plrtoban..""
		if game.Players:FindFirstChild(plrtoban) == nil then
			print("Not in server")
		else
			game.Players:FindFirstChild(plrtoban):Kick("You have been banned\n For: "..reason.."") -- Kick if they are in the server
		end
	else
		warn("Variable days returned nil.")
	end
end)

Also, try printing days before the conversion. If tonumber() cannot convert to a number (e.g. string is “day”), it will return nil.

I don’t have the part that fires the event but from what i can see you are probably passing the parameters in a wrong order resulting in days being nil

Yeah it seems like this variable is empty, otherwise it would print the number already, try checking if all the parameter are being passed to the function correctly.

I’ll send you that later!
I have to go for now.
mothers birthday

2 Likes
script.Parent.MouseButton1Click:Connect(function()
	script.Parent.Parent.Parent.Ban:FireServer(script.Parent.Parent.Parent.upper.Target.Text,script.Parent.Parent.Parent.upper.Reason.Text,script.Parent.Parent.Parent.upper.Time.Text,script.Parent.Parent.Parent.upper.TextLabel)
end)

Pretty simple, as I have not made a verification for it yet.

dear god please add a check so exploiters can’t ban players without admin perms

if not adminTable[plr.UserId] then
return
end

2 Likes

Hi @CoconutError , The problem that occurs in your error is simply due the “Days” Parameter that comes from the client.

So make sure to print days and check if that is a number, If it’s not a number then the problem lies with the client

Don’t worry! I’m going to add one to each function when they are all working! Thanks for the concern.

It says
Ban Length (days)
What should I do?

I know this wasn’t exactly what you were asking… but PLEASE. SECURE YOUR REMOTES.

Verify the RemoteEvent is being called by only a trusted caller. Verify that they have the permission to ban. You have the parameter plr which defines the Player who called it. Make a permission checker. Make a debouncer.

For example (instead of a DataStore), simply check a file that literally just returns a table of userIds.

local modList: {number} = require(script.ModList);

script.Parent.Ban.OnServerEvent:Connect(function(player: Player, playerBanning: string)
    if not table.find(modList, player.UserId) then
        -- user using the remote is a bad actor, punish them and drop the request
        return player:Kick("Tampering with Remotes");
    end

    local uid = game.Players:GetUserIdFromNameAsync(playerBanning);
    if not uid then
        -- player was invalid and does not actually exist
        return;
    end

    -- your ban logic here for DataStoreService (or other framework)

    if game.Players:FindFirstChild(playerBanning) then
        -- player was in the server and should be removed after banning
        game.Players[playerBanning]:Kick("You have been banned!");
    end
end)

Do not worry as I have said it earlier in the post.
Once all of my functions intended for my admin panel are working I am going to secure it the best I can.

But thanks for the script which I can use.

Can you show us, you’re local script of the ban panel?

Hey, all of a sudden when my team did a re test to see if any more issues came up the ban actually worked. But now the error is from the script which ensures if the player rejoins they are banned.
Error : ServerScriptService.Panel.PlayerJoin:7: attempt to index nil with number

Script
local config = require(game.ServerScriptService.Panel.AdminChecker.Admin.panel.Config)
local datastoreservice = game:GetService("DataStoreService") -- These two lines are the datastores
local bandata = datastoreservice:GetDataStore(config.DataStoreKey)

game.Players.PlayerAdded:Connect(function(plr) -- When a player joins the server
	local data = bandata:GetAsync(plr.UserId) -- Ban Data we stored
	if data[1] == plr.UserId then
		if os.time() >= data[2] then
			bandata:SetAsync(plr.UserId,{".",".","."})
		else
			local d = data[2] - os.time()
			local s = d / 86400 -- Days
			local f = math.round(s)
			plr:Kick("\n You have been banned\n For: "..data[3].."\n Day/Days Left Until Unban: "..f.."")
		end
	end
end)

But at the end the ban and unbanned worked perfectly.

Print the variable data value and send it here, if it’s nil/empty there was probably a mistake when saving the data

Yeah it printed as nil, how do I fix that?

Send me the script that saves players data

Here’s the Ban Handler.

Here
local config = require(script.Parent.Config)
local datastoreservice = game:GetService("DataStoreService") -- These two lines are the datastores
local bandata = datastoreservice:GetDataStore(config.DataStoreKey)


script.Parent.Ban.OnServerEvent:Connect(function(plr,plrtoban,reason,days,status) -- When you fire the server
	print(days)
	local uid = game.Players:GetUserIdFromNameAsync(plrtoban) -- If they aren't in the server, it gets the id anyway with no error unless it's an invalid name
	local ostime = os.time() -- Time
	local days = tonumber(days) -- Because we used a string, we need to convert it into a number for the multiplication to work
	local d = days * 86400 -- Multiplies by how many seconds are in a day
	local length = d + ostime -- Lengh of the ban
	local table2 = {uid,length,reason} -- I made a table here but you could of done bandata:SetAsync(uid,{uid,length,reason})
	bandata:SetAsync(uid,table2) -- Store the data
	status.Text = "Banned: "..plrtoban..""
	if game.Players:FindFirstChild(plrtoban) == nil then
		print("Not in server")
	else
		game.Players:FindFirstChild(plrtoban):Kick("You have been banned\n For: "..reason.."") -- Kick if they are in the server
	end
end)