Ban System won't work

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!

13 Likes

Maybe the duration is too small, so that when you rejoin, the time ban is already up?
If that doesn’t work, I’d advise using the good old “spam print” method, which is basically spamming prints after every “if” statement and “return”.

5 Likes

Ouhh, just a small error. (I think) Basically you did < instead of >

-- Wrong
if os.time() < banData.banEndTime then
-- Right
if os.time() > banData.banEndTime then

It is on the Ban Check on 6th line. Also you may do >=, it would be accurate.

6 Likes

I don’t think that the duration is small because I rejoin instantly.

4 Likes

For some reason, it didn’t work. I’ll try adding print statements to see what the problem is.

4 Likes

I would save the ban in the datastore BEFORE kicking the player in this case (since you’re testing it on yourself and are probably the only one on the server, the server could be shutting down due to no players before saving the ban).

Edit: I agreed with @nayro007 at first, but after reading through it again, you had it correct in the first place.

4 Likes

Oh, you right. I read it again, and changed my mind. Yeah I see now, I didn’t really saw the function to be honest, I accidently only looked on the if function. But about the saving, I do think it will still work, since game.Players.PlayerRemoving and the kick are similiar.

5 Likes

I’ve made my own custom admin commands before, this was one of the issues with banning I had as well :slight_smile:

2 Likes

I did what you said but still it won’t work. Tell me if I did your steps in a wrong way.

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({...}, " ")
				DataStores.Banned:SetAsync(targetPlayer.UserId, {banEndTime = os.time() + duration, reason = reason})
				targetPlayer:Kick("You were banned by an admin for "..duration.." seconds. 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()
		print("ban check done")
		return DataStores.Banned:GetAsync(userId)
	end)
	
	print(success, banData)
	if success and banData then
		print(success, banData)
		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)

EDIT: I forgot to tell you that I added two print statements. The first one prints “ban check done” and the second one prints “true false” (true is the success and false is the bandata)

4 Likes

Add a quick task.wait() before kicking the user. May seem like a stupid idea, but it can give the data time to be saved.

4 Likes

	["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({...}, " ")
				DataStores.Banned:SetAsync(targetPlayer.UserId, {banEndTime = os.time() + duration, reason = reason})
				task.wait(1)
				targetPlayer:Kick("You were banned by an admin for "..duration.." seconds. Reason: "..reason)
			else
				HUDMod.sendNotificationAdmin(player, "MULTIPLE PLAYERS FOUND", "error")
			end
		end
	};

Still won’t work.

You are kicked from the game, correct?

4 Likes

Yes, I am kicked from the game when I enter the command but not when I rejoin.

3 Likes

You got API on what I believe, and also, basically try banning yourself with a long duration. Join back, and print out the Datastore Time, so you can check, if its even logged.

3 Likes

I already tried 70+ seconds and also as I said previously, the ban data is false as the print says.

3 Likes

Ohh, wait. The problem is your pcall function. The second parameter returned should be error.

	local banData
	
	local success, err = pcall(function()
		print("ban check done")
		banData = DataStores.Banned:GetAsync(userId)
	end)
	
	print(success, banData)
	if success and banData then
		print(success, banData)
		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
3 Likes

Oh. Yeah I just realized that. But then what should I do to get the ban data?

3 Likes

I made it a variable in what I sent you, so try that out :slight_smile:

3 Likes

I mean, I also thought its a error. I didn’t know it would work like that, I tried it by printing it. And it worked, so I am not sure if error or something

3 Likes

How would I access it though? I don’t know much about pcall functions.

3 Likes