Hello! I am currently trying to make a rewards system where a player can claim an item every 4 hours, whether they are online or not. I am using a table saved within the DataStoreService each time, and I am using os.time() to save the time when they left + the time they spent playing. When they join, it will use these values to find out how long a player was offline for.
The data is stored in the variable 'playerDataDictionary`.
All this works completely fine in solo testing. However, as soon as multiple players are in the game (studio local server testing, Roblox server testing) it errors.
rewardsTimer.Value = playerDataDictionary["RewardsTimer"]
if rewardsTimer.Value == nil then
rewardsTimer.Value = os.time()
end
if playerDataDictionary["RewardsTimer"] < 5 then
playerDataDictionary["RewardsTimer"] = os.time()
end
pRewardsTimer.Value = math.floor((os.time() - playerDataDictionary["RewardsTimer"]) + playerDataDictionary["PlayedRewards"])
if rewardsTimer.Value < 0 then
rewardsTimer.Value = 0
end
Error: “Attempt to compare nil < number”
(from this line returning nil: pRewardsTimer.Value = math.floor((os.time() - playerDataDictionary["RewardsTimer"]) + playerDataDictionary["PlayedRewards"]))
I have tried extending the wait time on my BindToClose() function from 5 to 7 seconds. I have also searched restlessly in the hope of finding a solution.
In saving:
["RewardsTimer"] = os.time() --this is within the table
I think it may be related to the fact that the playerDataDictionary["RewardsTimer"] is sometimes nil when you’re trying to compare it to a number. This can happen when the key RewardsTimer is not present in the playerDataDictionary or is set to nil.
if type(playerDataDictionary["RewardsTimer"]) ~= "number" then
playerDataDictionary["RewardsTimer"] = os.time()
end
rewardsTimer.Value = playerDataDictionary["RewardsTimer"]
if (os.time() - playerDataDictionary["RewardsTimer"]) >= 5 then
playerDataDictionary["RewardsTimer"] = os.time()
end
pRewardsTimer.Value = math.floor((os.time() - playerDataDictionary["RewardsTimer"]) + playerDataDictionary["PlayedRewards"])
if rewardsTimer.Value < 0 then
rewardsTimer.Value = 0
end
Update: I think I found the issue. I changed the data store data was saved to and now it is working fine in studio testing with multiple players (I also wrapped it in a pcall() just in case). I think the problem is whenever I add a new key within the playerDataDictionary it won’t actually check to see if the key is nil or not, throwing this error. The reason it happened multiple times is because my failsafe for data loading was never set to false, meaning it never got saved as the new value. Thanks @GeoSailor for your help.