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

How can i allow this to stop withought breaking my loop?

I am making a script that makes the wooden wall have health for one of my games. I am making it glow a certain colour representing its current health for a few seconds. After writing script.Parent.Transparency = [Value] wait(0.01)

10 times I ran into a problem. It continues to loop for obvious reasons, its part of a while loop. I need to stop it from looping more than once but I cannot break the script as it would not turn red once the health goes lower. How can I prevent this script from looping without breaking the whole loop? Here is my workspace, extras, frames, panels and scripts are folders.

game
	workspace
		woodenwall
			Extras
				humanoid
				Hitbox
			Frames(These are parts not GUI's)
				Frame
				SubFrame
			Panels
			[Ton of parts i will not list as it is not important anyways]
			Scripts
				DestroyService


Here is the code I am having trouble with.

local humanoid = script.Parent.Parent.Extras:FindFirstChild('Humanoid')
local hitbox = script.Parent.Parent.Extras.HitBox
local pannels = script.Parent.Parent.Pannels
local frames = script.Parent.Parent.Frames
while true do
	wait(0.001)
	if humanoid.Health == 500 then
		
	else
		
	if humanoid.Health <= 300 then
		hitbox.BrickColor = BrickColor.new('New Yeller')
		hitbox.Transparency = 0.99
		wait(0.01)
		hitbox.Transparency = 0.98
		wait(0.01)
		hitbox.Transparency = 0.97
		wait(0.01)
		hitbox.Transparency = 0.96
		wait(0.01)
		hitbox.Transparency = 0.95
		wait(0.01)
		hitbox.Transparency = 0.94
		wait(0.01)
		hitbox.Transparency = 0.93
		wait(0.01)
		hitbox.Transparency = 0.92
		wait(0.01)
		hitbox.Transparency = 0.91
		wait(0.01)
		hitbox.Transparency = 0.90
		wait(0.01)
		hitbox.Transparency = 0.91
		wait(0.01)
		hitbox.Transparency = 0.92
		wait(0.01)
		hitbox.Transparency = 0.93
		wait(0.01)
		hitbox.Transparency = 0.94
		wait(0.01)
		hitbox.Transparency = 0.95
		wait(0.01)
		hitbox.Transparency = 0.96
		wait(0.01)
		hitbox.Transparency = 0.97
		wait(0.01)
		hitbox.Transparency = 0.98
		wait(0.01)
		hitbox.Transparency = 0.99
		wait(0.01)
		hitbox.Transparency = 1
		
		
	if humanoid.Health <= 100 then
		hitbox.BrickColor = BrickColor.new('Really red')
		
	if humanoid.Health <= 0 then
		hitbox:Destroy()
		for i, v in pairs(pannels:GetChildren())do
			wait(0.1)
			v:Destroy()
		end
		for i, v in pairs(frames:GetChildren())do
			wait(0.1)
			v:Destroy()
			
		end
	end
	end
	end
	end
end

Can someone please help me solve this problem? Thank you for your time :)

  • FruitPuunch

2 answers

Do this instead on the "If humanoid.Health == 500 then" Part

local stage1 = humanoid.Health = 500
local stage2 = humanoid.health = 300

if stage1 then
	--function
end

if stage2 then
	--function
end

Im not 100% sure this will work.

Hi.

There is a function specifically made for when the health of a humanoid changes. It's called Humanoid.HealthChanged.

I'm not sure if it'll work for you, but here's a mock up script showing how it works.

local humanoid = script.Parent.Parent.Extras:FindFirstChild('Humanoid')

humanoid.HealthChanged:connect(function(hp)
	if hp == 450 then
		print("Health is at 450.")
	elseif hp == 300 then
		print("You guessed it. 300 HP.")
	else
		print("The health is "..hp)
	end
end)	

It's a much more streamlined way of doing it and won't cause your game the hassle of constantly checking the health.

Thanks,

Explosion

Edit: I probably misread your question, so here's what you actually wanted.

Conditional statements can be put together.

For example, if you wanted it to do this fading thing between 300 HP and 250 HP, you'd just do something like if hp <= 300 and hp >= 250 then.

I hope this answered your question.

Thanks again,

Explosion

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