Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Humanoid.HealthChanged acting strangely. Can someone help?

So I've been working on a custom health regeneration script for my Terraria-like game, and i tried to make it so when your not taking damage, a timer will start, and when it ends you'll regen hp. (faster if your standing still). But its only outputs "cant regen until not taking damage" once. Please help.

-- Gradually regenerates the Humanoid's Health over time, but this time like in Terraria.

local waitt = 1 -- the time between each regen
local regen = 1 -- how much you regen
local timer = 1.5 -- how long after not taking any damage regen will start
local canregen = false

--------------------------------------------------------------------------------

local Character = script.Parent
local Humanoid = Character:WaitForChild'Humanoid'

--------------------------------------------------------------------------------

while true do
	if Humanoid.HealthChanged then 
		print "cant regen until not taking damage"
		timer = 1.5 
		canregen = false 
	else 
		if timer < 0 then 
			canregen = true 
			print "able to regen"
			return 
		else 
			timer = timer-0.1 
			canregen = false
			print ("cant regen, timer is at ".. timer)
			wait(0.1) 
		end
	end
	
	while Humanoid.Health < Humanoid.MaxHealth and canregen do
		if Humanoid.MoveDirection == Vector3.new(0, 0, 0) then waitt = 0.5 else waitt = 1 end
		Humanoid.Health = Humanoid.Health + regen
		wait(waitt)
	end
	
	Humanoid.HealthChanged:Wait()
end

1 answer

✓ Accepted answer

GENERAL PRACTICE

Make sure the damage being taken is done on the server or else this will only run once.

Use a repeat loop instead of the second while loop (inverse the values), since once the repeat loop hits its criteria, it is bypassed.

Use parentheses when using the :WaitForChild() function

ISSUES

Move the Humanoid.HealthChanged Event outside of the while loop since it has a certain time frame in which to run if placed inside the loop.

I added an extra variable "change" to take the place of the HealthChanged Event in the if-then statement.

The if-then inside the health changed makes sure to reset the value of "change"

Revised Server Script

local waitt = 1
local regen = 1
local timer = 1.5
local canregen = false
local change = false

local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")

Humanoid.HealthChanged:Connect(function(health)
	print "cant regen until not taking damage"
	timer = 1.5 
	canregen = false
	if Humanoid.Health ~= Humanoid.MaxHealth then
		change = true
	else
		change = false
	end
end)


while true do
    if change == false then 
		if timer < 0 then 
			canregen = true 
			print "able to regen"
		else 
			timer = timer - 0.1 
			canregen = false
			print ("cant regen, timer is at "..timer)
			wait(0.1) 
		end
	end

	repeat
		if Humanoid.MoveDirection == Vector3.new(0, 0, 0) then
			waitt = 0.5
		else
			waitt = 1
		end
		Humanoid.Health = Humanoid.Health + regen
		wait(waitt)
	until Humanoid.Health >= Humanoid.MaxHealth and canregen == false
end

1 comment

0

parenthesis arent Needed for waitforchild or findfirstchild

This discussion is preserved as it was. New answers, comments, and votes are closed.