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 5 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?

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

Log in to vote
1
Answered by 5 years ago

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)
0
many thanks. User#21499 0 — 5y
Ad
Log in to vote
0
Answered by 5 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:

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)

Answer this question