Help with using BanAsync

What do you want to achieve?
I’m trying to make my own admin panel that others (mainly my friends) can use in their own games, if they HAVE their own games. Trying to figure out how to finish my ban command!

What is the issue?
When I press the button to fire the remoteEvent to the server, it just does nothing and it doesn’t return any errors for me. I don’t even know how to use BanAsync with the new Ban API!

What solutions have I tried to far?
I tried looking into the API but what I read was completely useless considering how I found shouldBeBanned() and getNextBanDuration() as two functions, not knowing what they did or meant.

Here is the LocalScript firing the event:

local button = script.Parent
local userBox = button.Parent:FindFirstChild("User")
local reasonBox = button.Parent:FindFirstChild("Reason")
local event = button:FindFirstChild("BanEvent")

-- Early exit if required UI elements are missing
if not button or not userBox or not event then
	warn("BanClient: Missing required UI elements.")
	return
end

local debounce = false
local cooldown = 1 -- seconds

button.MouseButton1Click:Connect(function()
	if debounce then return end
	debounce = true

	local playerName = userBox.Text
	local reason = reasonBox and reasonBox.Text or ""
	event:FireServer(playerName, reason)

	task.wait(cooldown)
	debounce = false
end)

And here’s the code picking up the event

local button = script.Parent
local Event = button:FindFirstChild("BanEvent")
local Players = game:GetService("Players")

-- Early exit if required objects are missing
if not button or not Event then
	warn("BanServer: Missing required objects.")
	return
end

Event.OnServerEvent:Connect(function(sender, playerName, reason)
	if typeof(playerName) ~= "string" or playerName == "" then
		button.Text = "ERROR"
		task.wait(1)
		button.Text = "Ban"
		return
	end

	local target = Players:FindFirstChild(playerName)
	if target then
		local success, err = pcall(function()
			-- Ban permanently (duration = nil), or set duration in seconds if desired
			local Players = game:GetService("Players")

			if shouldBeBanned(sender) then
				local banHistoryPages = Players:GetBanHistoryAsync(sender.UserId)
				local duration = getNextBanDuration(banHistoryPages) -- Creator-implemented logic
				local config: BanConfigType = {
					UserIds = { sender.UserId },
					Duration = duration,
					DisplayReason = "You violated community guideline #5",
					PrivateReason = "Put anything here that the user should not know but is helpful for your records",
					ExcludeAltAccounts = false,
					ApplyToUniverse = true,
				}

				local success, err = pcall(function()
					return Players:BanAsync(config)
				end)
				print(success, err)
			end			
			
			Players:BanAsync(target.UserId, nil, reason or "You have been banned from the server.")
		end)
		if success then
			button.Text = "BANNED"
		else
			button.Text = "ERROR"
			warn("BanServer: BanAsync failed -", err)
		end
		task.wait(1)
		button.Text = "Ban"
	else
		button.Text = "ERROR"
		task.wait(1)
		button.Text = "Ban"
	end
end)

I can’t figure out what’s going on, and how to use BanAsync correctly. Can someone help me with this issue and help me what shouldBeBanned() and getNextBanDuration() both mean and what the code is for them?

1 Like

Goddam so you’re banning the user that the RemoteEvent is coming from :flushed_face:
Why not switch that out with Players:GetUserIdFrameNameAsync(playerName)

I’m banning the user by giving the remote-event the player name in which it uses to then find the player object.

Edit: Sender is the player object
I’m using “Sender.UserId”

What I’m wondering is why the remote-event doesn’t fire.

It should fire. Where are the scripts located? Have you added print statements occasionally in the LocalScript to see where it exactly stops working?

The scripts are located inside of a gui button parented to a gui that gets cloned to any client that has access to it (calculated by scripts)

basically admins with access to admin panel
ANOTHER EDIT: I’ve tried using print, and I already know where it stops working: From the :connect to the end of the script

Okay well if the Gui is being cloned on the server to the player if they are an admin (in which is correct), it will still give exploiters the ability to ban players as they still can see the RemoteEvent under the Gui. This means that in the OnServerEvent signal, you will have to verify that the player it is coming from (sender) is the intended admin. If not, you would return end (and possibly ban that player as they tried to fire a remote event probably via an exploit).

Also the UserIds property of BanConfigType contains all the users that you are trying to ban (up to 50). Currently you are using the sender’s userId. Change it to

UserIds = { target.UserId },

Oh yeah and same thing here ^^

and here

Players:GetBanHistoryAsync(sender.UserId)

Could using tags on a model help with identifying whos admins or not?

What model? Isn’t this about gui?

What specific use case too? You should just verify via script wether a player is an admin.

Bad:
–Store admins >> Replicate admins to models >> Check if an admin is tagged on a model >> Do Action

Good:
–Store admins >> Check if user is an admin >> Do Action

Wow you’re so creative for that. Make sure to use three backticks (```) to start and end a block of code so we can truly see how useless it is so we can clown on you

why in the hell my text were like that, i used ```

Make sure the 3 backticks ARE NOT on the same line as the code. Do the backticks then drop a line.
Bad: ```Code

Good:

Code

The thing is, I already implemented this.

What I’m wondering is what 2 functions mean and why the remote-event isn’t firing. This doesn’t really help, but thank you for trying I guess.

What script/localscript is trying to fire the remoteevent.

Did you copy this code from the documentation because I’m pretty sure it also mentions these 2 functions (shouldBeBanned & getNextBanDuration). Are they defined in your script (i know kinda wild to ask)

They aren’t defined, which is why I need help on WHAT DO THEY DO?

Oh sorry lol.

They are quite simple and could be modified in many ways. It is just an example that the documentation gave to help developers get a sense of how to use the API.

shouldBeBanned → You could simplify make this a function that will return false if the user is above a certain rank in a group (e.g. return false if they are an admin).

getNextBanDuration → Another custom function you can make to see the player’s ban history in your universe to determine how long they should be banned. Example: BanHistory is 0, maybe ban them for a day. If they have already been banned once, ban them for 3 days. If they have been banned twice before, ban them for a week, etc… You make it custom for you. Also sometimes you will want to ban the player forever in which you should probably have another textbox on the client so the admin banning the player can input how long they will be banned for

Ah, thank you- now can you help me fig why the remote-event isnt firing

Sure of course. Can you drop an image of the explorer so I can see the hierarchy of the instances? Also were the scripts you provided 100% of them?

Yes.

This is also within a package model, for your info: