-
What do you want to achieve? Keep it simple and clear!
I want to make my daily reward system work in the actual game. This is the tutorial I followed : https://www.youtube.com/watch?v=akHYPFUw7g4.
Here is my code:
Server Script: (I set the daily time to 10 seconds for testing purposes and also I have a separate script for the leaderstats system)
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local GlobalStats = DataStoreService:GetDataStore("GlobalStats_02")
local RewardPad = workspace:WaitForChild("Reward_Pad")
local RewardEvent = ReplicatedStorage:WaitForChild("RewardEvent")
local Reward = {"Points", 500}
local RespawnTime = 10
function ClaimReward(player)
if os.time() >= player.DailyReward.Value then
player.leaderstats[Reward[1]].Value += Reward[2]
player.DailyReward.Value = os.time() + RespawnTime
RewardEvent:FireClient(player, Reward[1], Reward[2])
else
RewardEvent:FireClient(player)
end
end
function PlayerAdded(player)
local dailyReward = Instance.new("NumberValue")
dailyReward.Name = "DailyReward"
dailyReward.Value = 0
dailyReward.Parent = player
local data
local success, warning = pcall(function()
data = GlobalStats:GetAsync(player.UserId)
end)
if success and data then
dailyReward.Value = data.DailyReward
end
end
function PlayerRemoving(player)
local success, warning = pcall(function()
GlobalStats:SetAsync(player.UserId, {
DailyReward = player.DailyReward.Value
})
end)
if not success then warn(warning) end
end
local Debounce = false
RewardPad.Pad.Touched:Connect(function(obj)
local player = game.Players:GetPlayerFromCharacter(obj.Parent)
if not player then return end
if Debounce then return end
Debounce = not Debounce
ClaimReward(player)
task.wait(0.1) Debounce = not Debounce
end)
Players.PlayerAdded:Connect(PlayerAdded)
for _, player in pairs(Players:GetChildren()) do
task.spawn(function()
PlayerAdded(player)
end)
end
Players.PlayerRemoving:Connect(PlayerRemoving)
Local Script:
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Player = Players.LocalPlayer
local RewardEvent = ReplicatedStorage:WaitForChild("RewardEvent")
local RewardPad = workspace:WaitForChild("Reward_Pad")
local PlayerGui = Player:WaitForChild("PlayerGui")
local DailyReward = PlayerGui:WaitForChild("DailyReward")
function formatTime(seconds)
return string.format("%02ih, %02im, %02is", seconds/60^2, seconds/60%60, seconds%60)
end
function RewardNotification(currency, reward)
if not currency and not reward then return end
DailyReward.Enabled = true
DailyReward.Notification.Text = "Daily Reward Claimed "..reward.." "..currency.."!"
task.delay(3, function()
DailyReward.Enabled = false
end)
end
function displayTime()
local timeRemaining = Player.DailyReward.Value
if timeRemaining < os.time() then return end
while timeRemaining > os.time() do
RewardPad.Display.Main.Frame.TimeRemaining.Text = formatTime(timeRemaining - os.time())
task.wait(1)
end
RewardPad.Display.Main.Frame.TimeRemaining.Text = "CLAIM!"
end
RewardEvent.OnClientEvent:Connect(RewardNotification)
Player.DailyReward.Changed:Connect(function()
displayTime()
end)
displayTime()
-
What is the issue? Include screenshots / videos if possible!
This system only works in studio but not in game.
Here is a video of it working in studio:
Here is a video of it not working in the game:
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I have tried YT and Devforum
Here is the model of it if you want to test it yourself : DailyRewardSystem - Roblox