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)