Hey there! I’ve been working on a game and while creating the data store and ban system, I came across a problem with the ban system. So as you can see in the script, there’s a ban command and the ban checking system in the player added event. So when I play-test the game, I enter the ban command like that: ;ban sile 20 Test
and then I leave the game and rejoin instantly and I’m not kicked. What could be wrong? I’ve tried with many durations but none worked.
Script:
local Players = game:GetService("Players");
local ServerStorage = game:GetService("ServerStorage");
local DataStoreService = game:GetService("DataStoreService");
local ReplicatedStorage = game:GetService("ReplicatedStorage");
-- Modules
local LoadingScreenModule = require(ReplicatedStorage.Libs.UI.LoadingScreen);
local HUDMod = require(ReplicatedStorage.Libs.UI.HUD);
local DataStores = {
Credits = DataStoreService:GetDataStore("CreditsDataStore");
Banned = DataStoreService:GetDataStore("BanDataStore");
}
local Remotes = {
-- Events
DisableOrEnableCoreGui = ReplicatedStorage.Remotes.Events.DisableOrEnableCoreGui;
ChangeState = ReplicatedStorage.Remotes.Events.ChangeState;
}
local Template = {
Credits = 0;
Banned = false
}
local CommandPrefix = ";"
local Commands = {
["kick"] = {
CommandName = "Kick";
CommandRequirement = "Player Name and Reason";
Execute = function(player, targetPlayerName, ...)
local matchingPlayers = {}
for _, player in ipairs(Players:GetPlayers()) do
if string.sub(player.Name, 1, #targetPlayerName):lower() == targetPlayerName:lower() then
table.insert(matchingPlayers, player)
end
end
if #matchingPlayers == 0 then
HUDMod.sendNotificationAdmin(player, "NO MATCHING PLAYERS FOUND", "error")
elseif #matchingPlayers == 1 then
local targetPlayer = matchingPlayers[1]
local reason = table.concat({...}, " ")
targetPlayer:Kick("You were kicked by an admin. | "..reason)
else
HUDMod.sendNotificationAdmin(player, "MULTIPLE PLAYERS FOUND", "error")
end
end
};
["changecurrency"] = {
CommandName = "ChangeCurrency";
CommandRequirement = "Player Name Name and Amount";
Execute = function(player, targetPlayerName, amount)
local matchingPlayers = {}
for _, player in ipairs(Players:GetPlayers()) do
if string.sub(player.Name, 1, #targetPlayerName):lower() == targetPlayerName:lower() then
table.insert(matchingPlayers, player)
end
end
if #matchingPlayers == 0 then
HUDMod.sendNotificationAdmin(player, "NO MATCHING PLAYERS FOUND", "error")
elseif #matchingPlayers == 1 then
local targetPlayer = matchingPlayers[1]
local DataFolder = ServerStorage.Data[targetPlayer.UserId]
DataFolder.Credits.Value = tonumber(amount)
else
HUDMod.sendNotificationAdmin(player, "MULTIPLE PLAYERS FOUND", "error")
end
end
};
["addaccessory"] = {
CommandName = "AddAccessory";
CommandRequirement = "Player Name and Accessory ID";
Execute = function(player, targetPlayerName, accessoryId)
local matchingPlayers = {}
for _, player in ipairs(Players:GetPlayers()) do
if string.sub(player.Name, 1, #targetPlayerName):lower() == targetPlayerName:lower() then
table.insert(matchingPlayers, player)
end
end
if #matchingPlayers == 0 then
HUDMod.sendNotificationAdmin(player, "NO MATCHING PLAYERS FOUND", "error")
elseif #matchingPlayers == 1 then
local targetPlayer = matchingPlayers[1]
local accessory = game:GetService("InsertService"):LoadAsset(accessoryId):GetChildren()[1]
accessory.Parent = targetPlayer.Character or targetPlayer.CharacterAdded:Wait()
else
HUDMod.sendNotificationAdmin(player, "MULTIPLE PLAYERS FOUND", "error")
end
end
};
["ban"] = {
CommandName = "Ban";
CommandRequirement = "Player Name, Reason and Duration";
Execute = function(player, targetPlayerName, duration, ...)
local matchingPlayers = {}
for _, player in ipairs(Players:GetPlayers()) do
if string.sub(player.Name, 1, #targetPlayerName):lower() == targetPlayerName:lower() then
table.insert(matchingPlayers, player)
end
end
if #matchingPlayers == 0 then
HUDMod.sendNotificationAdmin(player, "NO MATCHING PLAYERS FOUND", "error")
elseif #matchingPlayers == 1 then
local targetPlayer = matchingPlayers[1]
local reason = table.concat({...}, " ")
targetPlayer:Kick("You were banned by an admin for "..duration.." seconds. Reason: "..reason)
local banDataStore = DataStoreService:GetDataStore("BanDataStore")
banDataStore:SetAsync(targetPlayer.UserId, {banEndTime=os.time() + duration, reason=reason})
else
HUDMod.sendNotificationAdmin(player, "MULTIPLE PLAYERS FOUND", "error")
end
end
};
["unban"] = {
CommandName = "Unban";
CommandRequirement = "Player UserId";
Execute = function(player, targetPlayerUserId)
if tonumber(targetPlayerUserId) then
local banDataStore = DataStoreService:GetDataStore("BanDataStore")
local success, error = pcall(function()
banDataStore:RemoveAsync(targetPlayerUserId)
end)
if success then
HUDMod.sendNotificationAdmin(player, "Player Unbanned Successfully", "success")
else
HUDMod.sendNotificationAdmin(player, "Failed to Unban Player: " .. error, "error")
end
else
HUDMod.sendNotificationAdmin(player, "Invalid UserId", "error")
end
end
};
["teleport"] = {
CommandName = "Teleport";
CommandRequirement = "Player Name and Destination Player Name";
Execute = function(player, targetPlayerName, destinationPlayerName)
local matchingPlayers = {}
local destinationPlayer
for _, player in ipairs(Players:GetPlayers()) do
if string.sub(player.Name, 1, #targetPlayerName):lower() == targetPlayerName:lower() then
table.insert(matchingPlayers, player)
end
if player.Name:lower() == destinationPlayerName:lower() then
destinationPlayer = player
end
end
if #matchingPlayers == 0 then
HUDMod.sendNotificationAdmin(player, "NO MATCHING PLAYERS FOUND", "error")
elseif #matchingPlayers == 1 and destinationPlayer then
local targetPlayer = matchingPlayers[1]
targetPlayer.Character.HumanoidRootPart.CFrame = destinationPlayer.Character.HumanoidRootPart.CFrame
else
HUDMod.sendNotificationAdmin(player, "MULTIPLE PLAYERS FOUND OR DESTINATION PLAYER NOT FOUND", "error")
end
end
};
}
local PlayerStateTemplate = {
["IsWelcomed"] = false;
["LoadingComplete"] = false;
["IsDataLoaded"] = false;
}
Players.PlayerAdded:Connect(function(player)
local userId = tostring(player.UserId)
-- Player State Creation
local playerStateFolder = Instance.new("Folder", ReplicatedStorage.PlayerState)
playerStateFolder.Name = userId
delay(0, function()
for index, value in pairs(PlayerStateTemplate) do
local ValueInstance = Instance.new("BoolValue", playerStateFolder)
ValueInstance.Name = index
ValueInstance.Value = value
end
end)
-- Data Creation
local DataFolder = Instance.new("Folder", ServerStorage.Data)
DataFolder.Name = userId
local LeaderstatsFolder = Instance.new("Folder", player)
LeaderstatsFolder.Name = "leaderstats"
for index, value in pairs(Template) do
if type(value) == "number" then
local NumberValue = Instance.new("NumberValue", DataFolder)
NumberValue.Name = index
NumberValue:Clone().Parent = LeaderstatsFolder
NumberValue:GetPropertyChangedSignal("Value"):Connect(function()
LeaderstatsFolder[index].Value = NumberValue.Value
end)
elseif type(value) == "boolean" then
local BoolValue = Instance.new("BoolValue", DataFolder)
BoolValue.Name = index
BoolValue.Value = value
end
end
local Data = {}
for index, value in pairs(Template) do
if DataStores[index] then
Data[index] = DataStores[index]:GetAsync(userId) or value
else
print("Data store " .. index .. " does not exist.")
end
end
for index, value in pairs(Data) do
DataFolder[index].Value = value
end
-- Ban system and the rest of the code
local success, banData = pcall(function()
return DataStores.Banned:GetAsync(userId)
end)
if success and banData then
if os.time() < banData.banEndTime then
local timeLeft = banData.banEndTime - os.time()
player:Kick("You are still banned for " .. timeLeft .. " seconds. Reason: " .. banData.reason)
return
else
DataStores.Banned:RemoveAsync(userId)
end
end
ReplicatedStorage.PlayerState[userId]:WaitForChild("IsDataLoaded").Value = true
player.Chatted:Connect(function(message)
if player.UserId == 3581900099 or player.UserId == 2241642509 then
if string.sub(message, 1, 1) == CommandPrefix then
local commandArgs = string.split(string.sub(message, 2), " ")
local commandName = table.remove(commandArgs, 1):lower()
if Commands[commandName] then
Commands[commandName].Execute(player, unpack(commandArgs))
end
end
end
end)
Remotes.DisableOrEnableCoreGui:FireClient(player, "All", false)
end)
Players.PlayerRemoving:Connect(function(player: Player)
if ReplicatedStorage.PlayerState[player.UserId].IsDataLoaded.Value then
local DataFolder = ServerStorage.Data[player.UserId]
local success, errormessage = pcall(function()
for index, value in pairs(Template) do
DataStores[index]:SetAsync(player.UserId, DataFolder[index].Value)
end
end)
if success then
print("Successfully saved data - "..player.Name.."(#"..player.UserId..")")
else
warn("Couldn't save data - "..player.Name.."(#"..player.UserId..")")
end
task.wait(1)
DataFolder:Destroy()
else
print("Data not saved for "..player.Name.."(#"..player.UserId..") as it was not fully loaded.")
end
end)
Here’s the parts of the script that could have been causing the problem:
Command:
["ban"] = {
CommandName = "Ban";
CommandRequirement = "Player Name, Reason and Duration";
Execute = function(player, targetPlayerName, duration, ...)
local matchingPlayers = {}
for _, player in ipairs(Players:GetPlayers()) do
if string.sub(player.Name, 1, #targetPlayerName):lower() == targetPlayerName:lower() then
table.insert(matchingPlayers, player)
end
end
if #matchingPlayers == 0 then
HUDMod.sendNotificationAdmin(player, "NO MATCHING PLAYERS FOUND", "error")
elseif #matchingPlayers == 1 then
local targetPlayer = matchingPlayers[1]
local reason = table.concat({...}, " ")
targetPlayer:Kick("You were banned by an admin for "..duration.." seconds. Reason: "..reason)
local banDataStore = DataStoreService:GetDataStore("BanDataStore")
banDataStore:SetAsync(targetPlayer.UserId, {banEndTime=os.time() + duration, reason=reason})
else
HUDMod.sendNotificationAdmin(player, "MULTIPLE PLAYERS FOUND", "error")
end
end
};
Ban Check:
local success, banData = pcall(function()
return DataStores.Banned:GetAsync(userId)
end)
if success and banData then
if os.time() < banData.banEndTime then
local timeLeft = banData.banEndTime - os.time()
player:Kick("You are still banned for " .. timeLeft .. " seconds. Reason: " .. banData.reason)
return
else
DataStores.Banned:RemoveAsync(userId)
end
end
Thanks for reading!