BanAsync() Not Working

I tried and it didn’t work because it has to be a dictionary and it just says “Unable to cast to dictionary” in the output.

Sorry, didnt read that part in the API. I’m not sure why it wouldn’t be working if you have used math.floor. Maybe check to see if it is a number again.

It still says it’s not a number lol

image
This is how it is done on the docs, maybe check the order of the arguments or do something similar to this (make the table beforehand)

No, BanAsync requires the config properties to be defined. Handy dandy documentation.

Also, I’ve identified the problem. You’re sending the following variables into the RemoteEvent:
PlayerName, Message, Time
On the server, you’re catching these variables:
Player, Message, Time
The first parameter of OnServerEvent will always be the client that fired the event. If you want to correct the issue, you should do this instead.
Player, PlayerName, Message, Time

The problem all along was that the Time variable on the server script was being read as what you entered for Message.

Also, you should make sure that PlayerName actually belongs to a player on the server with game.Players:FindFirstChild(“PlayerName”).

Edit: I should mention that you should definitely look into methods to secure your remotes, as your current setup is extremely exploitable. The approach I would take is to make sure that the player that fired the event actually has the admin panel in their PlayerGui or that they’re on a list of users authorized to use it.

1 Like

Math.floor would work for the purpose of converting the duration to an integer, which is what I meant by that. Also nice catch, Im stupid for not seeing that.

“Config must contain field Duration with type number”

Do me a favor and immediately before the BanAsync line, add this:

print(typeof(Time))

Then let me know what that prints into output.

It’s a string (charscharscharschars)

I did tostring(Time) and it still didn’t work.

Would you mind re-posting your code of the server script after making the changes?

It’s the same, I just changed did the tostring() in the local script.

Server script:

game.ReplicatedStorage.Misc.BanPlayer.OnServerEvent:Connect(function(Player, PlayerName, Message, Time)
	game.Players:BanAsync({UserIds = {PlayerName.UserId}, ApplyToUniverse = true, Duration = Time, DisplayReason = Message, PrivateReason = "Banned", ExcludeAltAccounts = false})
end)

Local Script:

script.Parent.MouseButton1Up:Connect(function()
	local PlayerName = script.Parent.Parent.PlayerName.Text
	local Message = script.Parent.Parent.Message.Text
	local Time = tostring(script.Parent.Parent.Time.Text)

	game.ReplicatedStorage.Misc.BanPlayer:FireServer(PlayerName, Message, Time)
end)

…do the opposite… change it back to tonumber()…

already tried that once and still didn’t work.

Player maybe must be in game, try this code:

game.ReplicatedStorage.Misc.BanPlayer.OnServerEvent:Connect(function(Player, PlayerName, Message, Time)
	local playerToBan = game.Players:FindFirstChild(PlayerName)

	if playerToBan then
		local BanSettings = {
			UserIds = {playerToBan.UserId},
			Duration = Time,
			DisplayReason = Message,
			PrivateReason = "Banned",
			ExcludeAltAccounts = false,
			ApplyToUniverse = true,
		}
		game.Players:BanAsync(BanSettings)
	else
		warn("Player not found: " .. PlayerName)
	end
end)

Also don’t forget this in the LocalScript when Fire:

game.ReplicatedStorage.Misc.BanPlayer:FireServer(PlayerName, Message, tonumber(Time))
2 Likes

Quick fix but make sure its Player.UserId & not PlayerName.UserId. Def not the issue here but would likely be one later

Still "Config must contain field Duration with type number "

Can you show us your localscript code again?

game.ReplicatedStorage.Misc.BanPlayer:FireServer(PlayerName, Message, tonumber(Time))

That wouldn’t work because that would ban the player who send the command to ban the player, no the name of the p[layer they entered into the textbox.