Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Furniture damage function Humanoid 0 health to break?

Asked by 6 years ago

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?

1local children = workspace:GetChildren()
2 
3repeat
4    if script.Parent.Humanoid.Health == 0 or script.Parent.Humanoid.Health <= 0 then
5     for i,v in pairs(script.Parent:GetChildren()) do
6          v.Anchored = false
7end
8end
9until nil

2 answers

Log in to vote
1
Answered by 6 years ago

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

1while wait() do
2 
3end

or

1game:GetService("RunService").Heartbeat:Connect(function()
2 
3end)

My advice is do it like this:

01game:GetService("RunService").Heartbeat:Connect(function()
02    local h = script.Parent:FindFirstChild("Humanoid")
03    if h then
04        if h.Health <= 0 then
05            for i,v in pairs(script.Parent:GetChildren()) do
06                if (v:IsA("BasePart") or v:IsA("Union") or v:IsA("MeshPart")) then
07                    v.Anchored = false
08                end
09            end
10        end
11    end
12end)
0
many thanks. User#21499 0 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

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:

01local humanoid = script.Parent.Humanoid
02local sofa = script.Parent:GetChildren()
03 
04humanoid.HealthChanged:Connect(function(health)
05    if health <= 0 then -- Detect if health is low
06        -- Break sofa parts
07        for _,v in pairs(sofa) do -- Goes through children of sofa
08            if v:IsA("BasePart") or v:IsA("MeshPart") then -- Unanchors all parts
09                v.Anchored == false
10            end
11        end
12    end
13end)

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

1local humanoid = script.Parent.Humanoid
2local sofa = script.Parent
3 
4humanoid.HealthChanged:Connect(function(health)
5    if health <= 0 then -- Detect if health is low
6        sofa:BreakJoints() -- Breaks all welds
7    end
8end)

Answer this question