Hi, I’m a beginner developer learning how to script admin commands.
What do I want to achieve?
A ban/Unban script.
What is the issue?
The unbanning part doesn’t work.
I’ve tried many different solutions out there, none of them work for my script.
local datastore = game:GetService("DataStoreService")
local BanStore = datastore:GetDataStore("banstore111111")
local banCommand = "$ban ([%w_]+)"
local unbanCommand = "$unban ([%w_]+)"
local admins = {"81Frames"}
game.Players.PlayerAdded:Connect(function(plr)
local IsBanned = BanStore:GetAsync(plr.UserId.."-Banned") or false
if IsBanned then
plr:Kick("[Blox by itli] You have been banned.")
end
local executorName = "Unknown" -- Default value for the executor's name
plr.Chatted:Connect(function(msg)
local isAdmin = false
if isAdmin or table.find(admins, plr.Name) then
-- Ban command
local bannedPlrName = msg:match(banCommand)
if bannedPlrName then
executorName = plr.Name -- Set the executor's name
for i, p in pairs(game.Players:GetPlayers()) do
if string.lower(p.Name) == string.lower(bannedPlrName) or string.lower(p.DisplayName) == string.lower(bannedPlrName) then
local success, err = pcall(function()
BanStore:SetAsync(p.UserId.. "-Banned", true)
p:Kick("You have been banned by ".. executorName ..".")
warn("COMMAND EXECUTED: "..executorName.." has banned "..bannedPlrName)
end)
if err then
warn(err)
end
break
end
end
end
-- Unban command
local unbannedPlrName = msg:match(unbanCommand)
if unbannedPlrName then
executorName = plr.Name -- Set the executor's name
local success, err = pcall(function()
if game.Players:FindFirstChild(unbannedPlrName) then
BanStore:RemoveAsync(game.Players:GetUserIdFromNameAsync())
warn("COMMAND EXECUTED: " .. executorName .. " has unbanned " .. unbannedPlrName)
else
warn("WARNING: Player not found to be unbanned: " .. unbannedPlrName)
end
end)
if err then
warn(err)
end
end
end
end)
end)
You’re basically giving void to GetUserIdFromNameAsync, like there’s no argument here. You should do:
local PlayerId = game.Players:GetUserIdFromNameAsync(unbannedPlrName)
BanStore:SetAsync(PlayerId, false)
And uhh, rewriting the pcall, it could be:
local suc, Player = pcall(function()
return game.Players:FindFirstChild(unbannedPlrName)
end)
if Player == nil then
warn("Players doesnt exists!")
else
local PlayerId = game.Players:GetUserIdFromNameAsync(unbannedPlrName)
BanStore:SetAsync(PlayerId, false)
end
And also, I forgot to say, it will return If the player is on the server, not really if the player is a valid player. I recommend you to check if Player exists in Roblox as this way
local suc, Player = pcall(function()
return game.Players:GetUserIdFromNameAsync(unbannedPlrName)
end)
if Player == nil then
-- It return nil as default when this is not a valid PlayerId.
warn("Players doesnt exists!")
else
BanStore:SetAsync(Player , false)
end
Sorry for this reply,
I’m having trouble with the script. I’m getting an unknown global??
local unbannedPlrName = msg:match(unbanCommand)
if unbannedPlrName then
executorName = plr.Name – Set the executor’s name
local suc, Player = pcall(function()
return game.Players:GetUserIdFromNameAsync(unbannedPlrName)
end)
if Player == nil then
– It return nil as default when this is not a valid PlayerId.
warn(“Players doesnt exists!”)
else
BanStore:SetAsync(Player , false)
end
Can you take a screenshot of the output?, It’s weird, because its seems to be good. I mean, It’s the same index used to the bannedPlrName and If ban command work, this should work…
Oh, this make sense. msg is unknown global, because msg is an argument of Chatted event and it’s outside of that function. Paste the script and I’ll fix it
local unbannedPlrName = msg:match(unbanCommand)
if unbannedPlrName then
executorName = plr.Name -- Set the executor's name
local success, Player = pcall(function()
local playerId = game.Players:GetUserIdFromNameAsync(unbannedPlrName)
return playerId
end)
if success then
if Player == nil then
warn("Player doesn't exist!")
else
BanStore:SetAsync(Player, false)
warn("[Blox by itli] COMMAND EXECUTED: " .. executorName .. " has unbanned " .. unbannedPlrName)
end
else
warn(Player) -- `Player` will contain the error message in case there was an issue with `GetUserIdFromNameAsync`
end
end
end)
local datastore = game:GetService("DataStoreService")
local BanStore = datastore:GetDataStore("banstore111111")
local banCommand = "$ban ([%w_]+)"
local unbanCommand = "$unban ([%w_]+)"
local admins = {"81Frames"}
game.Players.PlayerAdded:Connect(function(plr)
local IsBanned = BanStore:GetAsync(plr.UserId.."-Banned") or false
if IsBanned then
plr:Kick("[Blox by itli] You have been banned.")
end
local executorName = "Unknown" -- Default value for the executor's name
plr.Chatted:Connect(function(msg)
local isAdmin = false
if isAdmin or table.find(admins, plr.Name) then
local bannedPlrName = msg:match(banCommand)
if bannedPlrName then
executorName = plr.Name
for i, p in pairs(game.Players:GetPlayers()) do
if string.lower(p.Name) == string.lower(bannedPlrName) or string.lower(p.DisplayName) == string.lower(bannedPlrName) then
local success, err = pcall(function()
BanStore:SetAsync(p.UserId.. "-Banned", true)
p:Kick("You have been banned by ".. executorName ..".")
warn("COMMAND EXECUTED: "..executorName.." has banned "..bannedPlrName)
end)
if err then
warn(err)
end
break
end
end
end
end
end)
-- Unban command
local unbannedPlrName = msg:match(unbanCommand)
if unbannedPlrName then
executorName = plr.Name -- Set the executor's name
local success, Player = pcall(function()
local playerId = game.Players:GetUserIdFromNameAsync(unbannedPlrName)
return playerId
end)
if success then
if Player == nil then
warn("Player doesn't exist!")
else
BanStore:SetAsync(Player, false)
warn("[Blox by itli] COMMAND EXECUTED: " .. executorName .. " has unbanned " .. unbannedPlrName)
end
else
warn(Player) -- `Player` will contain the error message in case there was an issue with `GetUserIdFromNameAsync`
end
end
end)
Assuming you only want to ban a player that is on the server, then use this
local datastore = game:GetService("DataStoreService")
local BanStore = datastore:GetDataStore("banstore111111")
local banCommand = "$ban ([%w_]+)"
local unbanCommand = "$unban ([%w_]+)"
local admins = {"81Frames"}
game.Players.PlayerAdded:Connect(function(plr)
local IsBanned = BanStore:GetAsync(plr.UserId.."-Banned") or false
if IsBanned then
plr:Kick("[Blox by itli] You have been banned.")
end
plr.Chatted:Connect(function(msg)
if table.find(admins, plr.Name) then --- If the player is an admin.
if msg:match(banCommand) then
local bannedPlrName = msg:match(banCommand)
local PlayerInCommand = game.Players:FindFirstChild(bannedPlrName)
if PlayerInCommand then -- If the player is on the server.
BanStore:SetAsync(PlayerInCommand.UserId.. "-Banned", true)
PlayerInCommand:Kick("You have been banned by ".. plr.Name ..".")
warn("COMMAND EXECUTED: "..plr.Name.." has banned ".. PLayerInCommand.Name)
else
warn("Player is not in the server!")
end
elseif msg:match(unbanCommand) then
local unbannedPlrName = msg:match(banCommand)
local success, PlayerId = pcall(function()
return game.Players:GetUserIdFromNameAsync(unbannedPlrName)
end)
if PlayerId == nil then
arn("This is not a valid player.")
else
BanStore:SetAsync(PlayerId, false)
warn("[Blox by itli] COMMAND EXECUTED: " .. plr.Name .. " has unbanned " .. unbannedPlrName)
end
end
end
end)
end)