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

Furniture damage function Humanoid 0 health to break?

So i made an Sofa* and it has Humanoid Thingy inside that model So i made if the health is 0 or is lower than 0 it brokes like every part anchored is going to be changed 0 but it does not seem to work can someone explain and fix this one?

local children = workspace:GetChildren()
		
repeat
	if script.Parent.Humanoid.Health == 0 or script.Parent.Humanoid.Health <= 0 then
     for i,v in pairs(script.Parent:GetChildren()) do
          v.Anchored = false
end
end
until nil

2 answers

✓ Accepted answer

First of all I wouldn't put the script in a repeat string until nil. Id advise using one of the below:

while wait() do

end

or

game:GetService("RunService").Heartbeat:Connect(function()

end)

My advice is do it like this:

game:GetService("RunService").Heartbeat:Connect(function()
	local h = script.Parent:FindFirstChild("Humanoid")
	if h then
		if h.Health <= 0 then
			for i,v in pairs(script.Parent:GetChildren()) do
				if (v:IsA("BasePart") or v:IsA("Union") or v:IsA("MeshPart")) then
					v.Anchored = false
				end
			end
		end
	end
end)

1 comment

0

many thanks.

There is a better way of detecting if the 'Sofa' health changes instead of running a loop. Humanoids have a special event on them which only fires when their health changes! This is useful in your situtation

The event looks like this:

local humanoid = script.Parent.Humanoid
local sofa = script.Parent:GetChildren()

humanoid.HealthChanged:Connect(function(health)
	if health <= 0 then -- Detect if health is low
		-- Break sofa parts
		for _,v in pairs(sofa) do -- Goes through children of sofa
			if v:IsA("BasePart") or v:IsA("MeshPart") then -- Unanchors all parts
				v.Anchored == false
			end
		end
	end
end)

If your sofa is 'welded' together you can do something even faster

local humanoid = script.Parent.Humanoid
local sofa = script.Parent

humanoid.HealthChanged:Connect(function(health)
	if health <= 0 then -- Detect if health is low
		sofa:BreakJoints() -- Breaks all welds
	end
end)

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