I’m trying to make a stat depletion, every _ seconds it will fire a remote to take from the players hunger/thirst, the issue is that they are on separate timers. (i.e you can lose hunger and thirst at different cooldowns).
while humanoid do
local configuration = character.Configuration
local hungerRate = configuration.HungerDepletionRate.Value
wait(hungerRate)
print("hunger")
remotes.Depletion:FireServer("Hunger")
end
while humanoid do
local configuration = character.Configuration
local thirstRate = configuration.ThirstDepletionRate.Value
wait(thirstRate)
print("thirst")
remotes.Depletion:FireServer("Thirst")
end```
How can I make it so that both of these run?
task.defer(function()
while humanoid do
local configuration = character.Configuration
local hungerRate = configuration.HungerDepletionRate.Value
wait(hungerRate)
print("hunger")
remotes.Depletion:FireServer("Hunger")
end
end)
while humanoid do
local configuration = character.Configuration
local thirstRate = configuration.ThirstDepletionRate.Value
wait(thirstRate)
print("thirst")
remotes.Depletion:FireServer("Thirst")
end```
How can I make it so that both of these run?
coroutine.wrap(function()
while humanoid do
local configuration = character.Configuration
local hungerRate = configuration.HungerDepletionRate.Value
wait(hungerRate)
print("hunger")
remotes.Depletion:FireServer("Hunger")
end
end)()
coroutine.wrap(function()
while humanoid do
local configuration = character.Configuration
local thirstRate = configuration.ThirstDepletionRate.Value
wait(thirstRate)
print("thirst")
remotes.Depletion:FireServer("Thirst")
end
end)()
You could use co-routines or task.spawn()
This example will use task.spawn
task.spawn(function()
while humanoid do
local configuration = character.Configuration
local hungerRate = configuration.HungerDepletionRate.Value
wait(hungerRate)
print("hunger")
remotes.Depletion:FireServer("Hunger")
end
end)
task.spawn(function()
while humanoid do
local configuration = character.Configuration
local thirstRate = configuration.ThirstDepletionRate.Value
wait(thirstRate)
print("thirst")
remotes.Depletion:FireServer("Thirst")
end```
end)?
I’m surprised I haven’t seen this yet, but you should consider simplifying it into one loop.
while humanoid do
local configuration = character.Configuration
for item: Folder in configuration:GetChildren() do
local depletionRate = item.DepletionRate.Value
local statToDeplete = item.StatToDeplete .Value
task.wait(depletionRate)
remotes.Depletion:FireServer(statToDeplete)
end
end
This assumes you restructure character.Configuration to have two folders both having two values DepletionRate and StatToDeplete
*also note the use of task.wait over wait, you can read on the differences here, but TL;DR use task.wait over wait ten times out of ten.
Oh yeah I really wasn’t thinking while writing that post, definitely can get it into one loop quite easily, though.’
Edit: Here is an extremely simple method using RenderStepped
local RunService = game:GetService("RunService")
local configuration = script.Parent:WaitForChild("Configuration")
-- Init stat ticks
for _, stat in configuration:GetChildren() do
stat.Tick.Value = tick()
end
-- Main loop
RunService.RenderStepped:Connect(function()
for _, stat in configuration:GetChildren() do
local lastTick = stat.Tick
local passed = tick() - lastTick.Value
local rate = stat.Rate.Value
if (passed < rate) then continue end
lastTick.Value = tick()
-- Hanlde stat depletion
print("passed", stat.Name)
end
end)
This is a mock setup so you’d have to change some variables around using your current structure
you can have two while loops but you have to remember while loops by themselves will halt any further code unless you use task.spawn(function() while loop end) this way it will be able to run both while loops but it is probably better to wrap it all inside a coroutine or runservice. But controll the timing for when hunger and thirst are updated using the step and other variables but hey. Im sure someone can show you a solution
It wouldn’t tho, you can handle all of them from one script, and there shouldn’t be any lag, maybe yes but ot would be so minimal you can’t feel it
Something like:
task.spawn(function()
while true do
for _, hungerTable in pairs(hungerTable) do
hungerTable.Value -= 1
task.wait(hungerRate)
end
end
end)
task.spawn(function()
while true do
for _, thirstTable in pairs(thirstTable) do
thirstTable.Value -= 1
task.wait(thirstRate)
end
end
end)
HungerTable and ThirstTable are both the tables containing all of the characters’ values for hunger and thirst