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!
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.
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)
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
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
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.
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)