I am making a daily reward system and this is the code I have:
The Code
local DataStore2 = require(1936396537)
local DataStore = game:GetService(“DataStoreService”):GetDataStore(“DailyRewards”)
local hourWait = 12
local possibleRewards = {100, 100,100, 1000, 1000, 1000, 1000, 1000, 10000, 10000, 10000, 100000, 100, 100}
game.Players.PlayerAdded:Connect(function(player)
local timeNow = os.time()
local data
pcall(function()
data = DataStore:GetAsync(player.UserId.."-dailyReward")
print("GettingData...")
end)
if data ~= nil then
local timeSinceLastClaim = timeNow - data
print("Time since last claim is "..timeSinceLastClaim)
if (timeSinceLastClaim / 3600) >= hourWait then
local reward = possibleRewards[math.rad(1,#possibleRewards)]
print("CODE55 "..reward)
game.ReplicatedStorage.DailyRewardEvent:FireClient(player, hourWait, reward)
local connection
connection = game.ReplicatedStorage.DailyRewardEvent.OnServerEvent:Connect(function(triggeringPlayer)
if triggeringPlayer == player then
print("Reward Claimed!")
local coinsStore = DataStore2("coins", player)
coinsStore:Increment(reward)
DataStore:SetAsync(player.UserId.."-dailyReward", os.time())
connection:Disconnect()
end
end)
else
print("Player is uneligible right now")
end
else
warn("Data ain't nil!")
local reward = possibleRewards[math.rad(1,#possibleRewards)]
print(reward)
game.ReplicatedStorage.DailyRewardEvent:FireClient(player, hourWait, reward)
local connection
connection = game.ReplicatedStorage.DailyRewardEvent.OnServerEvent:Connect(function(triggeringPlayer)
if reward == player then
print("Reward Claimed!")
local coinsStore = DataStore2("coins", player)
coinsStore:Increment(reward)
DataStore:SetAsync(player.UserId.."-dailyReward", os.time())
connection:Disconnect()
end
end)
end
end)
--[[game.Players.PlayerRemoving:Connect(function(player)
local data
pcall(function()
data = DataStore:GetAsync(player.UserId.."-dailyReward")
print("Getting Data...")
end)
if data == nil then
-- New player
pcall(function()
local timeVal = os.time()
DataStore:SetAsync(player.UserId.."-dailyReward")
end)
print("Saved because you are a new player")
end
end) --]]
It printed “Data ain’t nil” and right after it says the reward is nil. It is super confusing. Please help!
warn("Data ain't nil!")
local reward = possibleRewards[math.rad(1,#possibleRewards)]
print(reward)