I Am Making A Hourly Rewards System But I Don’t Know How To Use os.time() To Award The Player Every Hour Here Is The Script:
local Time = os.time()
local Label = script.Parent.TimeLabel
local IsActive = false
local player = game.Players.LocalPlayer
local function add()
player.leaderstats.Money.Value += 500
end
while wait(60) do
if os.time() - Time >= 3600 then
if not IsActive then
IsActive = true
add()
print(player.Name..' Got Reward!')
IsActive = false
end
end
end
Here it is the same script that is optimized for yours:
local Label = script.Parent.TimeLabel
local IsActive = false
local player = game.Players.LocalPlayer
local tick = tick()
local function add()
player.leaderstats.Money.Value += 500
end
while wait(60) do
if tick - tick() >= 3600 then
if not IsActive then
IsActive = true
add()
print('An hour has passed and' ..player.Name..' Got a Reward!')
IsActive = false
end
end
end
What? I’ve always heard reasons to why we should not use tick. The post you linked is a personal preference, and doesn’t rely on any fact. As far as I know it was going to get deprecated.
This is true because I don’t think that the intended behavior is to give rewards even when the player is offline (no datastores in the code). This means that even os.time is bad.
It’s because of shadowing. Rename the variable in line 4 to something like last. In the end your code should be like this:
local Label = script.Parent.TimeLabel
local IsActive = false
local player = game.Players.LocalPlayer
local last = os.clock()
local function add()
player.leaderstats.Money.Value += 500
end
while wait(60) do
if last - os.clock() >= 3600 then
if not IsActive then
IsActive = true
add()
print('An hour has passed and' ..player.Name..' Got a Reward!')
IsActive = false
end
end
end
By the way, why do you use an IsActive variable? It is unnecessary as no other code will execute in the meantime.
Change if last - os.clock() >= 3600 then to os.clock - last >= 3600.
For a more understandable script you can define a variable called hour_seconds with the initial value being 3600 and then you can just do if os.clock - last >= 1 * hour_seconds
Do not use tick() or os.Time. This can be hooked by exploiters and if you do it on server it will work on only one server. Absolutely store the time when player claimed reward in DataStoreService. And check it when player tries to claim reward again.
This Simple script You Can make player get 500 money every hour
Server:
game.Players.PlayerAdded:Connect(function(plr)
local timee = os.time()
while wait(60)do
if os.time() - timee >= 3600 then
plr.leaderstats.Money.Value += 500
game.ReplicatedStorage.Remote.Reward:FireClient(plr)
print("Working")
end
end
end)
Local Script:
game.ReplicatedStorage.Remote.Reward.OnClientEvent:Connect(function()
----What You You Do when Player Get Reward Or Money Example : Gui apper That Player Get Money
end)
I phrased my initial question wrong, I was asking “Are you trying to give them rewards every hour that they play straight?” it just didn’t register in my pea brain that people interpret that differently.