Why is my data Nil?

Hey so my Datastore seems to be nil even though I’m saving a number which prints out and shows its been saved. I divide and times by 1000 because it has a decimal point and OrderedDataStores are Int Values not Number Values. This is also just a rough copy I will not be saving and loading leaderboards every 60s.

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local TimeRecordBoard = game.Workspace.TimeLeaderBoard:WaitForChild("Display"):WaitForChild("WorldRecords")

local function ResetLeaderBoards()
	for i, frame in pairs(TimeRecordBoard.Frames:GetChildren()) do 
		local dataStore = DataStoreService:GetOrderedDataStore(frame.Name)
		local success, errormessage = pcall(function()
			local Data = dataStore:GetSortedAsync(true, 50)
			local Page = Data:GetCurrentPage()
			for rank, data in ipairs(Page) do 
				local Username = Players:GetNameFromUserIdAsync(tonumber(data.key))
				local Time = data.Value / 1000

				if Time then 
					local newFrame = frame:FindFirstChild("Template"):Clone()
					newFrame.UserName.Text = Username
					newFrame.Rank.Text = "#"..rank
					newFrame.Time.Text = tostring(Time)
					newFrame.Parent = frame
					newFrame.LayoutOrder = rank
					newFrame.Visible = true
					newFrame.Name = Username	
				else 
					print("No time was found")
				end
			end
		end)
		if not success then
			print(errormessage)
		elseif success then 
			print("Succesfully did leadeerboards")
		end
	end
end

local function saveData()
	for i, player in pairs(Players:GetPlayers()) do
		coroutine.wrap(function()
			for i, record in pairs(player:FindFirstChild("PersonalRecords"):GetChildren()) do 
				if record.Value < 100 and record.Value > 0 then 
					coroutine.wrap(function()
						local success = nil
						local errormessage = nil 
						local attempt = 1 
						
						local data = record.Value * 1000
						print(data)

						repeat
							success, errormessage = pcall(function()
								DataStoreService:GetOrderedDataStore(record.Name):SetAsync(player.UserId, data)
							end)
							attempt += 1
							if not success then 
								warn(errormessage)
								task.wait(3)
							end
						until success or attempt == 5
						if not success then 
							warn("Unable to save "..record.Name.." data for "..player.Name)
						else 
							print("Saved data")
						end
					end)()
				end
				
			end
		end)()
	end
end

while true do 
	ResetLeaderBoards()
	saveData()
	task.wait(60)
	for i, frame in pairs(TimeRecordBoard.Frames:GetChildren()) do
		for i, v in pairs(frame:GetChildren()) do 
			if v.Name ~= "Template" then 
				v:Destroy()
			end
		end
	end
end

image

image

Heres the error

and when im saving it, it prints out the right number
image

Your code seems correct, but I believe data.Value needs to be data.value, lowercase. Same for key. You can also get rid of ipairs since its generalized.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.