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")
end)

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.")
end

Here’s my code snippet for the PlayerAdded:

Players.PlayerAdded:Connect(function(plr)
	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]
		print(typeof(timeData))
	elseif data2[tonumber(plr.UserId)] == nil then
		for k, v in pairs(data2) do
			print(k, v)
		end
		print(data2[tonumber(plr.UserId)])
		print("No data for user " .. plr.Name)
	end
end)

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)
	end)
	if not yes and no then
		warn(no)
	elseif yes then
		print("BanIt | Successfully saved")
		for k, v in pairs(data2) do
			print(k, v)
		end
	elseif not yes then
		print("idk")
	end
end

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
	end
	local plr = Players:GetUserIdFromNameAsync(plrUser)
	print(typeof(plr))
	local current = os.time()
	data2[plr] = current .. ";" .. num
	print(data2[plr])
	saveTimeData()
	wait(1)
	if Players:FindFirstChild(plrUser) then
		Players[plrUser]:Kick("Banned for " .. num .. " " .. numType .. " from the game.")
	end
end

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:
image

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

Another screenshot:

image

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")
end)

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.

2 Likes

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.