Datastore Not Working Properly

For some reason, my DataStore isn’t working. It keeps saying that the player’s ban has expired, when it’s only been a few seconds or so. There are no errors in output, and I’m sure the CreateBan function is being called properly as I have used prints to debug.

local function CreateBan(Player,BanTime) 
	print(Player.Name)
	print(BanTime)
	local Success,Error = pcall(function()
		if BanTime == "one_day" then
			TempBanStore:SetAsync(Player.UserId,{BanStartTime = os.time(), BanTime = 86400})
			Player:Kick("You have been temporarily banned from Bondi Rescue Roleplay for one day.")
		elseif BanTime == "three_days" then
			TempBanStore:SetAsync(Player.UserId,{BanStartTime = os.time(), BanTime = 259200})
			Player:Kick("You have been temporarily banned from Bondi Rescue Roleplay for three days.")
		elseif BanTime == "five_days" then
			print("fivedaysbantime")
			TempBanStore:SetAsync(Player.UserId,{BanStartTime = os.time(), BanTime = 432000})
			Player:Kick("You have been temporarily banned from Bondi Rescue Roleplay for five days.")
		end
	end)
	if not Success then
		print("not success")
	end
end

game.Players.PlayerAdded:Connect(function(Player)
	local Success, Result = pcall(function() 
		return TempBanStore:GetAsync(Player.UserId,"TempBan") 
	end)
	
	if Success then 
		if Result then 
			if Result.BanStartTime + Result.BanTime > os.time() then 
				TempBanStore:RemoveAsync(tostring(Player.UserId), "TempBan")
				print("ban over")
			else
				Player:Kick("You have been temporarily banned from Bondi Rescue Roleplay.")  
				print("ban not over")
			end
		end
	end
end)

game.ReplicatedStorage.AdminPanelEvent.OnServerEvent:Connect(function(plr, bantype,evidence,reason,username)
	elseif bantype == "Temporary Bans - 5 Days" then
		CreateBan(game.Players:FindFirstChild(username), "five_days")
		local card = API:AddCard(username, "**"..bantype.."**".."\n\nUsername: "..username.."\n\nBanned By: "..plr.Name.."\n\nReason for Ban: "..reason.."\n\nEvidence: "..concattedevidence, ListID)
		local cardid = API:GetCardID(username, BoardID)
		API:PostComment(cardid, "Ban Added on "..months[ActualTime.month].." "..ActualTime.day..", "..ActualTime.year)
	end
end)

You’re using tostring here, but everywhere else you use just the ID. A number is different from a string.

> print(1 == "1")
false

Also Trello is not a database therefore shouldn’t be used like one.

1 Like

Hm. It wasn’t working before I added that line as well. I tried removing that line in particular and the issue persists.

The manner in which I’m using Trello in this instance is not as a database, but for logging purposes. It logs all of the evidence and proof onto Trello so that it can be easily accessed, while all bans are stored on the DataStore.

Your comparison statement is backwards. You’re checking if the total ban length (the start of the ban plus the ban time in seconds) is greater than the current time as the condition for the ban’s expiry, which will always be true in your case. After only a few seconds, the future time stamp will still be greater than the current time.

Remember that when you do StartTime + Length, you’re creating the minimum threshold of seconds that os.time needs to surpass for the ban to be considered over. When you check if that’s greater than the current time after a few seconds, naturally, it would be.

Try flipping that sign or just your operands altogether. When working with time-based utilities like this, I typically put the current time operand first because that makes it easier for me to see what exactly I’m doing and what’s being compared.

-- If the current time exceeds ban length (start + seconds = os.time threshold
-- to unban), then it's over and can be removed.
if os.time() > Result.BanStartTime + ResultBanTime then
2 Likes

In this scenario, the key’s datatype ultimately doesn’t matter because DataStore keys are coerced into strings under the hood. Any method you use will automatically tostring the key before reading or writing from DataStores. It’s still valid to remove a UserId key with tostring as much as it is to do so without it.

2 Likes