Attempt to index nil with 'Time'

Okay this will be a long one. I am attempting to make a daily rewards system that lets you claim a reward every 24 hours. Whenever I click the claim button, however, I get an error.

I apolagize for the sloppyness but when I get confused it starts getting messy

I have tried one sollution, which will be labeled in the script below, but anyway, here is the script:

-- There are the reward items
local Tools = game:GetService("ServerStorage"):WaitForChild("Tools")

local items = {
	["Bloxy Cola"] = Tools["Bloxy Cola"],
	["Cheeseburger"] = Tools.Cheeseburger,
}

local rewards = {15, 25,30, 35, 40, 45, 50, Tools["Bloxy Cola"], Tools["Cheeseburger"]}

-- Here is the remote event that fires the reward

game.ReplicatedStorage.Events.Reward.OnServerEvent:Connect(function(plr) -- plr is defined in the previous script as the players name
	local player = game:FindFirstChild(plr)
	
	local player_data

	local success, errorMessage = pcall(function()
		player_data = myDataStore:GetAsync(player_data)
	end)
	
	if player_data.Time then -- i added this to check if it exists, didn't work
		player_data.Time = os.time()
		
		local success, err = pcall(function()
			if (player_data.Time - player_data.Time) / 3600 >= 24 then

				local reward = rewards[math.random(1, #rewards)]

				if typeof(reward) == "number" then -- reward is an amount
					plr.leaderstats.Coin.Value = plr.leaderstats.Coins.Value + reward
				else -- reward is an item
					local tool = reward:Clone()
					tool.Parent = plr.StarterGear			
				end
				
				saveData(player, os.time())
			end	
		end)
		
		if success then
			print(plr.Name.." has claimed their daily reward")
		else
			print(plr.Name..": "..err)
		end
	end
end)

Here is where data is saved:

function saveData(plr, t)

	local playerUserId = plr.UserId -- Player key

	-- Data table
	local dataTable = {
		Coins = plr.leaderstats.Coins.Value;
		Gems = plr.leaderstats.Gems.Value;
		Tools = {};
		Time = t
	}

	-- Insert tools into dataTable
	for i, v in pairs(plr.StarterGear:GetChildren()) do -- After resseting, tools don't show up in your backpack
		table.insert(dataTable.Tools, v.Name)
	end

	local success, errorMessage = pcall(function()
		myDataStore:SetAsync(playerUserId, dataTable)
	end)

	if success then
		print(plr.Name.." ("..plr.UserId..")'s data has been loaded in")
	else
		print("Error saving data for "..plr.Name.." ("..plr.UserId..")")
		warn(errorMessage)
	end
end

(this function is run during player removing)

Have u tried accessing it with player_data[“Time”] maybe .
Can u access the other keys from the data table ? Is it just this one for time?

So a few things:


Firstly,

If you(as a developer) wanted to find out this statement then it might be best to abuse the Print() function as following:

if success then
  print(unpack(player_data))
end

If it prints then at least you’ve narrowed down your problems, however if it doesn’t print then obviously you end the function as the player hasn’t even saved anything that can be grabbed using :GetAsync() and unless someone’s dedicated to stay in a server for 24 hours then it’s likely a bug with your saving


Secondly, avoid using the same variable for multiple things, even for pcall().
In your first script you used 2 pcall functions with same success variable which should be obvious to you why that’s an issue but if it’s not then it’s because it causes confusion in the script.


Lastly, another method of doing this is using the tick() function, however it can be very inefficient and straightforward. I recommend you only use it when all hope is lost.

I can access everything in the data table except time, its the only one that has issues