How do I make a Offline Progress System?

So, on Some tycoons, simulators and other types of games, I noticed when you leave you keep grinding even if your offline, or online, which means a offline progress.

I always searched many ways of how to make a offline progress system but If somebody knows how to make one please help me!

4 Likes

I would store the time they leave with DataStoreService. When they join, you can calculate how much they made while they were away.

2 Likes

Oh, like creating a certain value to the player? One problem I get confused about is by how does it make a addition since its not gonna run the script if the player is not on the game.

1 Like

You won’t do anything while they are offline. You will calculate how much they made while they were away when they join back.

What I mean by is, when the player leaves the game, by using the game.Players.PlayerRemoving function, does it need to create a while loop to add time constantly, or?

When they are leaving the game, you will save the tick() to their datastore and on playeradded, you will write to their stats with how much they made by calculating based on the time. (multiplier * rate * time)

1 Like

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)
1 Like

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.

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…