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.
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.
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 You can do things like table.insert(bannedPlayers, newlyBannedId) to update your table!
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.
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"})
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.