No data in a DataStore is detected, even though there is data

I’ve been working on a Timed Ban for my banning module BanIt - Simple ban module for anyone, and I’m having trouble with saving data (namely a dictionary) to a DataStore.

Here’s what retrieves the data:

local succ, data2 = pcall(function()
	return timedBanStore:GetAsync("TimedBans")

if succ and not data2 then
	data2 = {}
	print("There was no data")
elseif not succ then
	warn("DataStore failed. Try turning on Studio Access to API Services.")

Here’s my code snippet for the PlayerAdded:

	if table.find(data, plr.UserId) or table.find(serverBanTable, plr.UserId) then
		plr:Kick("Banned from the game!")
	elseif data2[tonumber(plr.UserId)] ~= nil then
		local timeData = data2[plr]
	elseif data2[tonumber(plr.UserId)] == nil then
		for k, v in pairs(data2) do
			print(k, v)
		print("No data for user " .. plr.Name)

These together tell me that there is a piece of data within the DataStore. However, it always takes the data2 == nil route.

Here’s what saves the data:

local function saveTimeData()
	local yes, no = pcall(function()
		return timedBanStore:SetAsync("TimedBans", data2)
	if not yes and no then
	elseif yes then
		print("BanIt | Successfully saved")
		for k, v in pairs(data2) do
			print(k, v)
	elseif not yes then

and here’s how it’s implemented:

function BanIt.TimedBan(plrUser, num, numType)
	if numType:lower() == "minutes" then
		num *= 60
	elseif numType:lower() == "hours" then
		num *= 3600
	elseif numType:lower() == "days" then
		num *= 86400
	local plr = Players:GetUserIdFromNameAsync(plrUser)
	local current = os.time()
	data2[plr] = current .. ";" .. num
	if Players:FindFirstChild(plrUser) then
		Players[plrUser]:Kick("Banned for " .. num .. " " .. numType .. " from the game.")

Any help with this? My mind is boggled right now.

try wrapping the dictionary in game:GetService(“HttpService”):JSONEncode(tbl) when you save and retrieve the data with game:GetService(“HttpService”):JSONDecode(tbl)

This didn’t work, any other suggestions?

How are the functions arranged in the script (from top to bottom). If they are out of order it could be a problem.

It’s shown in the order that it is shown on the post.

1 Like

When you print “Data2[plr]” does anything get printed?

You mean here?

It prints the os.time value, “;”, and the number of seconds.

My main problem might be retrieving the data. When I iterate through it here:

it prints my user ID and the os.time value;number, but it says that data2[plr.UserId] is nil.

No I mean here, in the BanIt.TimedBan function:

Yeah, that is what I originally quoted.

No you quoted your PlayerAdded event. This is the function were it actually bans the player.

If you click the arrow, it will show the highlighted area was my TimedBan function. Anyway, like I said:

Oh sorry I forgot to click the arrow! (Also it still isn’t printing when the player joins in my module even when I join from the Roblox player)

1 Like

Did you execute a TimedBan on yourself?

Anyway here is a screenshot of the issue:

This proves there is data that is under my user Id, but it refuses to acknowledge that it’s there.

Another screenshot:


1 Like

You should try predefining Data2 as nil and then instead of returning data2 in the Pcall you redefine it as the loaded data. Like so:

local data2 = nil
local succ, error = pcall(function()
	data2 = timedBanStore:GetAsync("TimedBans")

then just replace if succ and not data2 with if succ and not error

I think what was happening is you were setting data2 to be the error message which will be nil if there are no problems.

No I was replying to the wrong topic.

1 Like

one thing, why are you returning here?

woudn’t that overwrite error messages?
anyways, try using [OPEN SOURCE] Free DataStore Editor Plugin! - Resources / Community Resources - DevForum | Roblox to see if you can find any data inside that key.


It does not overwrite error messages (have tested this method on Ro-Chat). Also, like I said here,

I think the error might be here:

Wow, this was embarrassingly simple. Instead of wrapping it in a tonumber:

it should be wrapped in tostring()

In the beginning you didn’t even had to wrap it into a number anyway lmao.