How Would I Make My Hourly Rewards Give Rewards Every Hour Using os.time()?

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

People may be more useful than me, but I’d highly suggest this page:
os.clock() may be something you should be using instead of os.time() :+1:

https://developer.roblox.com/en-us/api-reference/lua-docs/os

1 Like

Use tick instead as aforementioned in here:

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

1 Like

I Got An Error Saying:

attempt to call a number value

Which lines causes that error?

It Says Its Coming From Line 11

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.

2 Likes

For Some Reason This Script Doesn’t Work Either

Oh, I just noticed it.

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.

Dont Make any Of This On Local Script Just in Server Cuz Can Easily Hacker Can Increase His Money From This Script

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)
2 Likes

Are you trying to give them rewards every hour that they play straight? Or every hour they play in general?

1 Like

There’s a difference between every hour they play and every hour they play straight

1 Like

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.

1 Like