So recently I’ve been wanting to do my own ban command script, it does kick players when I use the kick and ban command (Haven’t tested cross-servers yet), but it doesn’t “ban” the player.
Basically the ban command is working like the kick command but what I want it to do is banning the player permanently not just kick the player.
Chat Script:
local commands = {
"!kick",
"!ban",
"!unban"
}
local banModule = require(script.BanModule)
game.Players.PlayerAdded:Connect(function(player)
local currentBanList = banModule:GetList() or {}
if currentBanList[player.UserId] then
print(#currentBanList)
player:Kick(currentBanList[player.UserId])
end
-- Ban command
if player.UserId == 1418534298 then
player.Chatted:Connect(function(message)
local splitUpCommand = string.split(message, " ")
local theCommand = table.find(commands, splitUpCommand[1])
if theCommand then
local victim = splitUpCommand[2]
if victim == "AstralBlu_e" or victim == "XAnimeX2" then return end
local reason = string.sub(message, (#splitUpCommand[1] + #splitUpCommand[2] + 2 - #message))
if splitUpCommand[1] == "!kick" then
banModule:kick(victim, reason)
elseif splitUpCommand[1] == "!ban" then
banModule:ban(currentBanList, victim, reason)
elseif splitUpCommand[1] == "!unban" then
banModule:unban(currentBanList, victim)
end
end
end)
end
end)
Module:
local banModule = {}
local DSS = game:GetService("DataStoreService")
local banData = DSS:GetDataStore("Ban_Data")
local messagingService = game:GetService("MessagingService")
local acrossServerBanKey = "ServerBanMessage"
local function messagingService(bannedPersonID, reason)
local theCurrentReason
local subConnect
local thePlayer
local messageSuccess = pcall(function()
subConnect = messagingService:SubscribeAsync(acrossServerBanKey, function(message)
local playerAndBanReasonTable = message.Data
for i,v in pairs(playerAndBanReasonTable) do
thePlayer = game.Players:GetPlayerByUserId(i)
theCurrentReason = v
end
end)
end)
if messageSuccess then
if thePlayer then
thePlayer:Kick(theCurrentReason)
subConnect:Disconnect()
end
end
local publishMessageSuccess, publishErrorMessage = pcall(function()
messagingService:PublishAsync(acrossServerBanKey, {[bannedPersonID] = reason})
end)
if not publishMessageSuccess then print(publishErrorMessage) end
end
function banModule:ban(currentBanList, victim, reason)
local success, errormessage
local t = 0
local bannedPersonID = game.Players:GetUserIdFromNameAsync(victim)
currentBanList[bannedPersonID] = reason
if game.Players:GetPlayerByUserId(bannedPersonID) then
game.Players:GetPlayerByUserId(bannedPersonID):Kick(reason)
else
messagingService(bannedPersonID, reason)
end
repeat
success, errormessage = pcall(function()
banData:SetAsync("BanList", currentBanList)
end)
if success then
t = 3
print("Successfully banned!")
else
t = t + 1
warn(errormessage)
end
until t == 3
end
function banModule:unban(currentBanList, victim)
local success, errormessage
local t = 0
currentBanList[game.Players:GetUserIdFromNameAsync(victim)] = nil
repeat
success, errormessage = pcall(function()
banData:SetAsync("BanList", currentBanList)
end)
if success then
t = 3
else
t = t + 1
warn(errormessage)
end
until t == 3
end
function banModule:GetList()
local success, errormessage
local banList
local t = 0
repeat
success, errormessage = pcall(function()
banList = banData:GetAsync("BanList")
end)
if success then
t = 3
return banList
else
t = t + 1
warn(errormessage)
end
until t == 3
end
function banModule:kick(victim, reason)
local kickedPersonID = game.Players:GetUserIdFromNameAsync(victim)
if game.Players:GetPlayerByUserId(kickedPersonID) then
game.Players:GetPlayerByUserId(kickedPersonID):Kick(reason)
else
messagingService(kickedPersonID, reason)
end
end
return banModule