Hello,
I am making a Daily Reward System for my game, I tried following AlvinBlox’s tutorial on how to make one, everything was going well until I tested my game, and got no results. Here is the Script and LocalScript I wrote.
Script:
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local TimeDataStore = DataStoreService:GetDataStore("TimeDataStore")
local RewardDataStore = DataStoreService:GetDataStore("RewardDataStore")
local hourTime = 0.00001
local Reward = 900
local EventGive = game:GetService("ReplicatedStorage").RewardEvents.GiveReward
local EventSave = game:GetService("ReplicatedStorage").RewardEvents.SaveReward
local function playerAdded(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local seconds = Instance.new("IntValue")
seconds.Name = "Seconds"
seconds.Value = 900
seconds.Parent = leaderstats
if seconds.Value > 0 then
seconds.Changed:Connect(function()
if seconds.Value < 1 then
seconds.Value = 0
end
end)
end
if seconds.Value < 999999999999 then
seconds.Changed:Connect(function()
if seconds.Value > 999999999999 then
seconds.Value = 999999999999
end
end)
end
local success, value = pcall(function()
return TimeDataStore:GetAsync(tostring(plr.UserId))
end)
if success and value then
seconds.Value = value
else
seconds.Value = 900
end
seconds.Parent = leaderstats
while true do
wait(1)
plr.leaderstats.Seconds.Value = plr.leaderstats.Seconds.Value - 1
end
local timeNow = os.time()
local data
pcall(function()
data = RewardDataStore:GetAsync(plr.UserId)
end)
if data ~= nil then
local lastSession = timeNow - data
if (lastSession / 3600) >= hourTime then
EventGive:FireClient(plr, hourTime, Reward)
local connection
connection = EventSave.OnServerEvent:Connect(function(triggerPlr)
if triggerPlr == plr then
RewardDataStore:SetAsync(plr.UserId, os.time())
connection:Disconnect()
end
end)
else
return nil
end
else
EventGive:FireClient(plr, hourTime, Reward)
local connection
connection = EventSave.OnServerEvent:Connect(function(triggerPlr)
if triggerPlr == plr then
RewardDataStore:SetAsync(plr.UserId, os.time())
connection:Disconnect()
end
end)
end
end
local function playerLeaving(plr)
local success, err = pcall(function()
TimeDataStore:SetAsync(tostring(plr.UserId), plr.leaderstats.Seconds.Value)
end)
if not success then
warn("Failed to save Timer data for player " .. plr.Name .. ": " .. err)
end
local data
pcall(function()
data = RewardDataStore:GetAsync(plr.UserId)
end)
end
Players.PlayerAdded:Connect(playerAdded)
Players.PlayerRemoving:Connect(playerLeaving)
game:BindToClose(function()
task.wait(10)
end)
LocalScript:
local GiveEvent = game:GetService("ReplicatedStorage").RewardEvents.GiveReward
local SaveReward = game:GetService("ReplicatedStorage").RewardEvents.SaveReward
local TextLabel = script.Parent
local Sound = script.Parent.RewardSound
local Player = script.Parent.Parent.Parent.Parent
local Stat = Player:WaitForChild("leaderstats"):WaitForChild("Seconds")
local TS = game:GetService("TweenService")
local TI = TweenInfo.new(5, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0)
local Tween = TS:Create(TextLabel, TI, {TextTransparency = 1})
local Tween2 = TS:Create(TextLabel, TI, {BackgroundTransparency = 1})
function rewardReceived()
TextLabel.BackgroundTransparency = .75
TextLabel.TextTransparency = 0
Sound:Play()
Stat.Value = Stat.Value + 900
task.wait(5)
Tween:Play()
Tween2:Play()
end
GiveEvent.OnClientEvent:Connect(function(lastSession, Reward)
rewardReceived()
SaveReward:FireServer()
end)
The code is a bit modified since I made it my way. If you can help me or want more information please reply. (I’m still new to coding so I don’t have ideas on how to fix it.)