Admin Panel doesn't working

so i made a admin panel based on new roblox Ban API, but seems like nothing work.

This is the script:

	local banMessage = script.Parent.Frame.Reason.Text
	local DurationText = script.Parent.Frame.Duration.Text
	local userName = script.Parent.Frame.PlayerName.Text
	local userId = game.Players:GetUserIdFromNameAsync(userName)
	if userId then
		game.Players:BanAsync({
			UserIds = {userId},
			ApplyToUniverse = true,
			Duration = DurationText,
			DisplayReason = banMessage,
			PrivateReason = banMessage,
			ExcludeAltAccounts = false,
		})
	end
end)
script.Parent.Frame.Unban.Activated:Connect(function()
	local userName = script.Parent.Frame.PlayerName.Text
	local userId = game.Players:GetUserIdFromNameAsync(userName)
	if userId then
		game.Players:UnbanAsync({
			UserIds = {userId},
			ApplyToUniverse = true,
		})
	end
end)

and this is screenshot of the gui itself:
image

so basically I want this to work, can i get help?

Use a local script instead or modules

thank for possible solution, i will try it

It works, but banAsync works on servers, it doesnt work with local scripts

Something like this should work.

Code
local banMessage = script.Parent.Frame.Reason.Text
local DurationText = script.Parent.Frame.Duration.Text
local userName = script.Parent.Frame.PlayerName.Text

local function getBanDuration(durationText)
    local duration = tonumber(durationText)
    if duration then
        return duration
    elseif durationText == "permanent" then
        return -1
    else
        return 0
    end
end

local function getUserIdFromUsername(username)
    local success, userId = pcall(function()
        return game.Players:GetUserIdFromNameAsync(username)
    end)
    if success and userId then
        return userId
    else
        warn("Failed to get UserId for username: " .. username)
        return nil
    end
end

script.Parent.Frame.ban.Activated:Connect(function()
    local userId = getUserIdFromUsername(userName)
    if userId then
        local banDuration = getBanDuration(DurationText)
        if banDuration then
            local config = {
                UserIds = {userId},
                Duration = banDuration,
                DisplayReason = banMessage,
                PrivateReason = banMessage,
                ExcludeAltAccounts = false,
                ApplyToUniverse = true
            }
            
            local success, err = pcall(function()
                return game.Players:BanAsync(config)
            end)
            
            if success then
                print("User " .. userName .. " successfully banned.")
            else
                warn("Error banning user " .. userName .. ": " .. err)
            end
        else
            warn("Invalid duration for ban: " .. DurationText)
        end
    end
end)

script.Parent.Frame.Unban.Activated:Connect(function()
    local userId = getUserIdFromUsername(userName)
    if userId then
        local success, err = pcall(function()
            return game.Players:UnbanAsync({
                UserIds = {userId},
                ApplyToUniverse = true,
            })
        end)
        
        if success then
            print("User " .. userName .. " successfully unbanned.")
        else
            warn("Error unbanning user " .. userName .. ": " .. err)
        end
    end
end)

maybe you should be using Duration = tonumber(DurationText) instead of Duration = DurationText if the duration needs to an integer.

also, you can’t use :BanAsync in local scripts so exploiters cant ban people

sadly, this doesnt work, not even shows the error, warn or print

about banasync, you right, but when i use it in normal scripts, its just doesnt working

have you tried using Duration = tonumber(DurationText)?

if that isn’t working, try printing the different variables (banMessage, userName etc.) to make sure that they are correctly being passed into :BanAsync.

sadly, but still shows nothing if use normal script, like you said i shouldnt use local script bc exploiters

what is being printed when you print all of the variables?

also, are there any errors?

1)what you mean by what is being printed when you print all of the variables?
2) no, there is absolutely nothing shows up

sorry, i meant can you print the different variables (banMessage , userName etc.) to make sure that they are correctly being passed into :BanAsync?

still nothing, when i press ban or unban it still doing nothing

are you using .Activated for the ban?, .Activated is client-sided

1 Like

yea, you should be using .MouseButton1Down rather than .Activated

oh, ok i will try that one, forgot about that exist

you can use a local script and RemotEvents RemoteEvent | Documentation - Roblox Creator Hub to handle the GuiButton pressed on the client, just send a signal to the server, verify the user has access to use commands and you’ll be good!

but what if i want to ban be activated on mobile or smth?

.MouseButton1Down fires for all devices