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?
1 | local children = workspace:GetChildren() |
2 |
3 | repeat |
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 |
7 | end |
8 | end |
9 | until nil |
First of all I wouldn't put the script in a repeat string until nil. Id advise using one of the below:
1 | while wait() do |
2 |
3 | end |
or
1 | game:GetService( "RunService" ).Heartbeat:Connect( function () |
2 |
3 | end ) |
My advice is do it like this:
01 | game: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 |
12 | end ) |
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:
01 | local humanoid = script.Parent.Humanoid |
02 | local sofa = script.Parent:GetChildren() |
03 |
04 | humanoid.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 |
13 | end ) |
If your sofa is 'welded' together you can do something even faster
1 | local humanoid = script.Parent.Humanoid |
2 | local sofa = script.Parent |
3 |
4 | humanoid.HealthChanged:Connect( function (health) |
5 | if health < = 0 then -- Detect if health is low |
6 | sofa:BreakJoints() -- Breaks all welds |
7 | end |
8 | end ) |