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

How do I make my npc's body parts dissapear on death?

Asked by 4 years ago

So when my npc's die their body parts scatter how would I remove this? Main script for animations/movement is here. https://pastebin.com/Awe5RYGm The script is so big it cant be posted but also this is a follow script so when it detects players it will follow the player if you need or want it.

function findNearestPlayer(Position)
    wait(0.3)
    local List = game.Workspace:GetChildren() --it's :GetChildren() not :Children()
    local Target --Just need this
    local Distance = 20

    for i, v in pairs(List) do --loops through all entries in List
        local humanoid = v:FindFirstChild("Humanoid") --Checks for a humanoid
        local hrp = v:FindFirstChild("HumanoidRootPart") --Checks for a RootPart

        if humanoid and hrp and v ~= script.Parent then --If it has both and v is not the script's parent
            local dist = (Position - hrp.Position).Magnitude 
            if dist <= Distance and humanoid.Health > 0 then
                local plr = game.Players:GetPlayerFromCharacter(v)
                if plr then --If it's a player
                    Target = hrp --we have our target!
                end
            end
        end
    end
    return Target
end


while wait() do --don't want to cause a timeout
    local target = findNearestPlayer(script.Parent.HumanoidRootPart.Position)
    if target then
        script.Parent.Humanoid:MoveTo(target.Position, target)
    end
end

2 answers

Log in to vote
0
Answered by 4 years ago

add this script anywhere in the character.

local humanoid = script.Parent:FindFirstChild()
if humanoid  == nil then
    warn("Humanoid is nil!")
end
humanoid.Died:Connect(function()
    local Char = script.Parent
    Char:Destroy()

end)
1
Just as a note if this npc is meant to respawn then that method won't work as the humanoid will be destroyed therefore the .Died event will be disconnected johndeer2233 439 — 4y
0
yeah, you could just keep a copy in server storage and copy that and place the npc where it respawns, keeping this script in it as well marine5575 359 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Try inserting a script inside of the npc that says:

script.Parent.Humanoid.Died:Connect(function()
    script.Parent:Destroy()
end)

also make sure no part of your npc is anchored

Answer this question