I am making a ban admin command

I am trying to make a ban admin command, I want the admin to be able to input a custom ban reason so I am using a folder with string values inside and the name of the string value is the player’s user id and the value is the reason.

I have put my user id in one of the values in order to test but it doesn’t work.

game.Players.PlayerAdded:Connect(function(user)
	for i, ban in pairs(script.Bans:GetChildren()) do
		if user.UserId == ban.Name then
			user:Kick(ban.Value)
		end
	end
end)

I have searched on forum but I can’t find anything.

2 Likes

UserIds are integers, and Names are strings, you cannot compare them.

> print(1 == "1")
false

You need to convert the user.UserId to a string or convert the ban.Name to a number.
tostring(user.UserId)
or
tonumber(ban.Name)

1 Like

You will want to use DataStores to store the banned players.

When they join access the store and check to see if they are banned.
This also means players can be banned in game without editing scripts manually.

The issue with your current ban system is that you are doing this:

The userId is not the players name, it is numbers.
You would want to check the players name to see if it is the ban name.

1 Like

Ah, that makes a lot of sense, thanks :+1:

You should probably make it a datasave. Well that’s what i’d do. I’d set up a datasave and set a value to 0. If that value is higher than 0 they are banned and will be kicked.

1 Like

I am going to use data stores for permanent bans.

ban.Name is the banned player’s UserId, he says so here:

You should probably set a script with all the names of players that are banned. and if they join they get an instakick

You should probably use UserId as the user could change their name to bypass the ban.

You could also make it ban players for a set amount of time if you use os.time to set a time period where the player will be banned.

Overall you would want to convert to a DSS ban system as it offers a lot more and is easier to use.

Yes I am planning on using data stores for time bans and perm bans but for server bans I am just going to use a folder seen as it will reset when the server gets shut down.

I highly agree with the other replies here, I gave a solution to your problem, but the way you’re approaching this isn’t efficient in terms of scalability or maintainability. Consider a datastore or hardcoding UserIds in a table in your script

local bannedPlayers = { -- table of integers
    743974327,
    234543234,
    654309812,
--etc
}

game.Players.PlayerAdded:Connect(function(player)
    for _, banned in pairs(bannedPlayers) do
        if banned == player.UserId then
            player:Kick("Banned")
        end
    end
end)

With this, creating instances of stringvalues is completely unnecessary, instead you’re saving the UserIds in a table within your script directly :slight_smile: You can do things like table.insert(bannedPlayers, newlyBannedId) to update your table!

Yeah this is the solution you need. @WinkleSociety

Yes but how would I go about adding in a custom reason for each player, this is for an admin script I just tested it by putting in my id, so I want the admin to be able to input a reason.

The idea is it is just a temp ban from that server only.

You may wanna make this change

local bannedPlayers = { – table of integers
743974327,
234543234,
654309812,
–etc
}

game.Players.PlayerAdded:Connect(function(player)
for _, banned in pairs(bannedPlayers) do
if banned == player.UserId then
player:Kick(“Banned”)
elseif banned = --customid then
player.Kick(“You have been banned for toxicity”)
end
end
end)

That’s as simple as updating your table to do essentially what you’re doing with StringValues

local bannedPlayers = {
    {
        ID = 3425432,
        Reason = "Ban reason goes here"
    }, -- don't forget the comma!
    {
        ID = 234324432,
        Reason = "A different reason goes here"
    }
}

Then all you have to do is reflect this change on your PlayerAdded script. Keep in mind that banned is now a table of tables.

game.Players.PlayerAdded:Connect(function(player)
    for _, banned in pairs(bannedPlayers) do
        if banned.ID == player.UserId then
            player:Kick(banned.Reason)
        end
    end
end)

For adding new values:
table.insert(bannedPlayers, {ID = PlayerIDHere, Reason = "Ban reason here"})

2 Likes

Thank you :grinning:

(And everyone else who contributed an answer)

2 Likes

I do have a completed ban Script that uses datastores and can set reasons for bans.
It also has a temp ban function that bans a player for 1 day.

I looked up ways to find players based on the start of their name so you don’t always have to type out the full name.
It could be a good reference for when you create your own ban script.

I would like to add that I have no issues with you using this script or parts or it so long as you full understand it.

1 Like

I have tried with table.insert but it isn’t working, I’m going to try and find a solution.

Could you elaborate on the “isn’t working” part? :slight_smile: