Hunger/thirst depletion

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?
2 Likes

It would be easiest to just make 2 different scrips.

5 Likes

try this

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

You can use coroutine.wrap and write the loop inside the the wrap function

@PiercedMissile has a good solution, but if you want only one script, use spawn. You can add anonymous functions within them like this:

print("started")
spawn(function()
	wait(1)
	print("waited!")
end)
wait(0.5)
print("this comes before waited")

check out coroutines!

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

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.

Pretty sure you can’t have two while scripts inside the same script, to fix this you can either make two separate scripts or use coroutine.wrap().

He has two while loops. That’s all I need to say. BTW still good job using task.wait()

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
image

You’re doing yourself a disservice by handling this on the client in the first place.

2 Likes

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 :slight_smile:

Handling it on the server would create massive amounts of lag as it would be doing this for each player character.

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

Yes but they run on different wait times, not the same. Wouldn’t this just be a global wait time?

No because the wait times are different as you can see

Also it’s better if you just keep the wait times on the server, maybe as a child of the script which should be on the server

What this does is simply wait a different time for both and then deplete the thirst/hunger for every Player