I need help with datastores

Every time I wipe the data store for this and I join for the first time it shows this error

https://gyazo.com/f730f39cb226fe3decf87731eb34d612

But then when I rejoin again it doesn’t show the error. I was wondering how I would fix it where I wouldn’t have to rejoin for it to work correctly here’s the script

local Version = 0.5;
local savedLeaderboardData = dataStoreService:GetDataStore("Leaderboard Time Remaining" .. Version);

function metaData:startTimer(player, totalTime)
	local savedTotalTime = savedLeaderboardData:GetAsync(tostring("Player 1"))
	
	if not savedTotalTime then
		print('new')
		savedTotalTime = savedLeaderboardData:SetAsync(tostring("Player 1"), totalTime);
	end
	
	savedTotalTime = tonumber(savedTotalTime);
	
	while wait(1) do
		savedTotalTime = savedTotalTime - 1;
		local Days =
		local Hours = math.floor(savedTotalTime / 3600 % 24);
		local Minutes = math.floor((savedTotalTime/60) % 60);
		local Seconds = math.floor(savedTotalTime % 60);
		
		local mainMap = game.Workspace.Maps["Main Map"];
		local leaderboard = mainMap.GlobalRap.Leaderboard.Board;
		local counterText = leaderboard.Canvas.Counter;
		
		if string.len(Days) == 1 then
			Days = "0" .. Days;
		end
		if string.len(Hours) == 1 then
			Hours = "0" .. Hours;
		end					
		if string.len(Minutes) == 1 then
			Minutes = "0" .. Minutes;
		end
		if string.len(Seconds) == 1 then
			Seconds = "0" .. Seconds;
		end		
		
		counterText.Text = "Resets in " .. Days .. ":" .. Hours .. ":" .. Minutes .. ":" .. Seconds;
		
		if totalTime == 0 then
			return nil;
		end
	end
end

If anyone could tell me why or how to fix this or if i can even fix it, it would be very helpful. Thank you :smile:

May you pin-point the line that is causing the error?

It has something to with these lines of code

local savedTotalTime = savedLeaderboardData:GetAsync(tostring("Player 1"))

if not savedTotalTime then
	print('new')
	savedTotalTime = savedLeaderboardData:SetAsync(tostring("Player 1"), totalTime);
end

savedTotalTime = tonumber(savedTotalTime);

while wait(1) do
	savedTotalTime = savedTotalTime - 1;

I had a similar error on something similar, I think it’s because you’re trying to add a number and nil (or nothing I think). You may need to add a nil safeguard somehow.

Your error does say that you are preforming arithmetic on nil and number, so I assume its

causing the problem.

Yes, but once I rejoin the game it works fine. It has something to with the datastores?

I think the data is generated on the 2nd join, if you clear your data, you have a nil value as your datastore (your key’s save anyway)

EDIT: Because I don’t want to spam, here’s my next answer: add a nil check when you get the data from the datastore. That way if it’s nil you can catch it.

It doesn’t return something when you are doing SetAsync.

The reason why is because if a data doesn’t exist, it will return nil when a player joins the first time. Now, when you rejoined again, there’s already a current data.

(PS: Also, please wrap your SetAsync and GetAsync in a pcall)

1 Like

How would I make it a current data without rejoining?

local savedTotalTime = savedLeaderboardData:GetAsync("Player 1")

if not savedTotalTime then
	print('new')
	savedLeaderboardData:SetAsync("Player 1", totalTime);
end

savedTotalTime = tonumber(totalTime)

while wait(1) do
	savedTotalTime = savedTotalTime - 1;
function metaData:startTimer(totalTime)
	local savedTotalTime = savedLeaderboardData:GetAsync("Player 1")
	
	if not savedTotalTime then
		print('new')
		savedTotalTime = savedLeaderboardData:SetAsync("Player 1", totalTime);
	end
	
	savedTotalTime = tonumber(savedTotalTime);
	
	while wait(1) do
		savedTotalTime = savedTotalTime - 1;
		local Days = 
		local Hours = math.floor(savedTotalTime / 3600 % 24);
		local Minutes = math.floor((savedTotalTime/60) % 60);
		local Seconds = math.floor(savedTotalTime % 60);
		
		local mainMap = game.Workspace.Maps["Main Map"];
		local leaderboard = mainMap.GlobalRap.Leaderboard.Board;
		local counterText = leaderboard.Canvas.Counter;
		
		if string.len(Days) == 1 then
			Days = "0" .. Days;
		end
		if string.len(Hours) == 1 then
			Hours = "0" .. Hours;
		end					
		if string.len(Minutes) == 1 then
			Minutes = "0" .. Minutes;
		end
		if string.len(Seconds) == 1 then
			Seconds = "0" .. Seconds;
		end		
		
		counterText.Text = "Resets in " .. Days .. ":" .. Hours .. ":" .. Minutes .. ":" .. Seconds;
		
		if totalTime == 0 then
			return nil;
		end
	end
end

[/quote]

Still having the same issue as before

I don’t see the new changes you did, in which I can’t help you without seeing the new changes.

My bad I fixed it you just removed the tostring()

Also I published it and its showing a different error than before