How do I make a Offline Progress System?

So, when the player enters, It creates a value that adds time per second (+1 per 5 seconds)

Does it looks like this?

game.Players.PlayerRemoving:Connect(function(player)
        local Result = tick(1, 1, player.Stats.Time.Value)
end)

I would recommend you save tick with datastore. ( tick() → seconds since a long time ago ) When they join back, you can multiply the rate by the difference of (tick() - leaveTime)

What you could do is create a datastore and save the time it was when they logged off. Then, when they log back on, subtract the time they logged off from the current time.

You could then use that to determine what amount of money to award the player.

Edit: Just realized that Tomate already mentioned this :slight_smile:

game.Players.PlayerAdded:Connect(function(player)
        local save = ds:GetAsync(player.UserId)
        
        Time.Value = (tick() - save) -- [ save is that value that i saved!
end)

game.Players.PlayerRemoving:Connect(function(player)
         ds:SetAsync(player.UserId,tick())
end)

Sorry If it looks a bit confusing, I didn’t put the rest of the leaderstats because its boring lol

1 Like

You should probably use a pcall but I think that should work and you would just multiply the time.value by the rate of their coins or whatever currency your game has.

1 Like

Ok, I dont use multiplier a lot in my games but thanks for your help! I will test it later.

Hey, so I tried making a script and it justs returns 0, I try to print the TimeSaved Value from the table, and it literally doesnt prints, heres the script I made so far.

local ds = game:GetService('DataStoreService'):GetDataStore('leaderstats')

game.Players.PlayerAdded:Connect(function(Player)
	local save = ds:GetAsync(Player.UserId)
	local leaderstats = Instance.new('Folder',Player)
	leaderstats.Name = 'leaderstats'
	
	local money = Instance.new('IntValue',leaderstats)
	money.Name = 'Money'
	
	local multiplier = Instance.new('IntValue',leaderstats)
	multiplier.Name = 'Multiplier'
	multiplier.Value = 1
	
	local Time = Instance.new('IntValue',leaderstats)
	Time.Name = 'Time'
	
	if save ~= nil then
		money.Value = save[1]
		multiplier.Value = save[1]
		local ThirdPosition = save[3]
		print(ThirdPosition.TimeSaved)
		Time.Value = (multiplier.Value * 1 * ThirdPosition.TimeSaved)
		money.Value += Time.Value
	else
		money.Value = 1
		multiplier.Value = 1
		local ThirdPosition = save[3]
		print(ThirdPosition.TimeSaved)
		Time.Value = (multiplier.Value * 1 * ThirdPosition.TimeSaved)
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local save = {}
	for _,Child in pairs(Player:WaitForChild('leaderstats'):GetChildren()) do
		if Child.Name == 'Time' then
			local Result = {}
			Result.TimeSaved = tick()
			table.insert(save,3,Result)
		else
			table.insert(save,Child.Value)
		end
	end
end)

You never wrote to the datastore.

Wdym? I store the tick in a value of a table that is inserted into the save. This is quite confusing.

He has a datastore, but he isn’t writing to it.

oh my god im so dumb, I literally forgot about the SetAsync() I was so nervous to test it so I just forgot it lol.

2 Likes

Just use datastore:SetAsync(Player.UserId, save) in the scope that you have the save table.

1 Like

I just wanna know if the tick saving is right, or not? Just that.

bro… I just got 1.6B money by just some seconds…

here

The tick() is in seconds. I don’t think that you got the change in time. I’m pretty sure you just used tick() (Tick is seconds since the unix epoch in 1970 and is a very large number)

Yeah, That was while Its going to save the time, and when the player joined it did this (multiplier.Value * 1 * ThirdPosition.TimeSaved). What did I do wrong?

Change it to (multiplier.Value * (tick() - ThirdPosition.TimeSaved))

1 Like

Yeah, Tysm! Its finally working properly now. Sorry If I said or replied too much

I believe tick is on the road to deprecation. Use DateTime.now for a futureproof solution.

2 Likes