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)