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