How can I decrease a value based on how long the player has been offline?

I’ve made a fatigue system in my game that increases when you train and decreases after doing nothing for a while. However, I want to make this value decrease while the player is offline or away from the game but I am struggling to use os.time. I did find other scripts that can calculate the time the player has been away from the game as seen in this post : How can I find how long it's been since a player plays my game last? - #12 by dlIl_0, but I still struggle to apply this to my current script and decrease its value by 0.01 every 3 minutes the player has been away.

local DataStoreService = game:GetService("DataStoreService")
local FatigueStore = DataStoreService:GetDataStore("BodyFatigueStore_1")
local RunService = game:GetService("RunService")
local canClose = false

game.Players.PlayerAdded:Connect(function(plr)
	local fatigueFolder = Instance.new("Folder")
	fatigueFolder.Parent = plr
	fatigueFolder.Name = "FatigueFolder"

	local fatigue = Instance.new("NumberValue")
	fatigue.Name = "BodyFatigue"
	fatigue.Parent = fatigueFolder
	
	local ID = "Player_"..plr.UserId

	--Load Data

	local data
	local success, errmsg = pcall(function()
		data = FatigueStore:GetAsync(ID)
	end)
	
	if success then
		fatigue.Value = data
	end

end)

game.Players.PlayerRemoving:Connect(function(plr)
	local iD = "Player_"..plr.UserId

	local data = plr.FatigueFolder.BodyFatigue.Value

	local success, errmsg = pcall(function()
		FatigueStore:SetAsync(iD, data)
	end)
	
	canClose = true
	
end)

game:BindToClose(function()
	
	while RunService:IsRunning() and not canClose do
		task.wait()
	end
	
	for i,v in pairs(game.Players:GetPlayers()) do
		
		local iD = "Player_"..v.UserId

		local data = v.FatigueFolder.BodyFatigue.Value

		local success, errmsg = pcall(function()
			FatigueStore:SetAsync(iD, data)
		end)
		
	end
end)

You will have to save the value returned by os.time() when your player leaves the game. When a player joins your game again you can get the current time by calling os.time() again and calculate the delta time in seconds that passed since your player left by subtracting the old time from the current one. As an example let’s say your player leaves the game and you save the os.time() time stamp 1718096634. Then when your player joins at for example the os.time() time stamp 1718097634 you can calculate the time passed in seconds by subtracting the old time from the current time like this: 1718097634 - 1718096634, which will in this case result in 1000 seconds passed since your player last played. From there you can use that number to calculate by how much you want to decrease your fatigue value.

2 Likes

But what if the Player joins and leaves server A and then joins back at server B, Won’t the os.time between both server differ?

os.time() returns the time since the Unix epoch which is always calculated using the UTC time zone. In turn this also means that the calculation works regardless of where a server is located.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.