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

Add regen to this humanoid script?

Asked by 10 years ago

I have been trying at it all night to add a regen to this script's humanoid when it dies or gets parented to nil. Here is the source:

function waitForChild(parent, childName)
    local child = parent:findFirstChild(childName)
    if child then return child end
    while true do
        child = parent.ChildAdded:wait()
        if child.Name==childName then return child end
    end
end

local function weldBetween(a, b)
    local weld = Instance.new("Motor6D")
    weld.Part0 = a
    weld.Part1 = b
    weld.C0 = CFrame.new()
    weld.C1 = b.CFrame:inverse()*a.CFrame
    weld.Parent = a
    return weld;
end

local function jointBetween(a, b, cfa, cfb)
    local weld = Instance.new("Motor6D")
    weld.Part0 = a
    weld.Part1 = b
    weld.C0 = cfa
    weld.C1 = cfb
    weld.Parent = a
    return weld;
end

    local parent = workspace:FindFirstChild("Dummy")
    if (parent == nil) then
        print("making dummy")
        parent = Instance.new("Model", game.Workspace)
        parent.Name = "SuperNoob"       
    end

    for index, child in pairs(parent:GetChildren()) do
        if (child.Name ~= "AnimSaves") then
            child:Destroy()
        end
    end

    --

    Root = Instance.new("Part", game.Workspace)
    wait(0.1)
    Root.Name = "HumanoidRootPart"
    Root.FormFactor = "Symmetric"
    Root.Anchored = false
    Root.CanCollide = false
    Root.Transparency = 1
    Root.Size = Vector3.new(2, 2, 1)
    Root.CFrame = CFrame.new(0, 5.2, 4.5)
    Root.Parent = parent
    Root.BottomSurface = "Smooth"
    Root.TopSurface = "Smooth"

    Torso = Instance.new("Part", game.Workspace)
    wait(0.1)
    Torso.Name = "Torso"
    Torso.FormFactor = "Symmetric"
    Torso.Anchored = false
    Torso.CanCollide = true
    Torso.Size = Vector3.new(2, 2, 1)
    Torso.CFrame = CFrame.new(0, 5.2, 4.5)
    Torso.Parent = parent
    Torso.BottomSurface = "Smooth"
    Torso.TopSurface = "Smooth"

    RCA = CFrame.new(0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 1, 0 )
    RCB = RCA
    RootHip = jointBetween(Root, Torso, RCA, RCB)
    RootHip.Name = "Root Hip"
    RootHip.MaxVelocity = 0.1


    LeftLeg = Instance.new("Part", game.Workspace)
    wait(0.1)
    LeftLeg.Name = "Left Leg"
    LeftLeg.FormFactor = "Symmetric"
    LeftLeg.Anchored = false
    LeftLeg.CanCollide = true
    LeftLeg.Size = Vector3.new(1, 2, 1)
    LeftLeg.CFrame = CFrame.new(0.5, 3.2, 4.5)
    LeftLeg.Parent = parent
    LeftLeg.BottomSurface = "Smooth"
    LeftLeg.TopSurface = "Smooth"

    LHCA = CFrame.new(-0.5, -1, 0) * CFrame.fromAxisAngle(Vector3.new(0, 1, 0), -math.pi/2)
    LHCB = CFrame.new(0, 1, 0) * CFrame.fromAxisAngle(Vector3.new(0, 1, 0), -math.pi/2)
    LeftHip = jointBetween(Torso, LeftLeg, LHCA, LHCB)
    LeftHip.Name = "Left Hip"
    LeftHip.MaxVelocity = 0.1


    RightLeg = Instance.new("Part", game.Workspace)
    wait(0.1)
    RightLeg.Name = "Right Leg"
    RightLeg.FormFactor = "Symmetric"
    RightLeg.Anchored = false
    RightLeg.CanCollide = true
    RightLeg.Size = Vector3.new(1, 2, 1)
    RightLeg.CFrame = CFrame.new(-0.5, 3.2, 4.5)
    RightLeg.Parent = parent
    RightLeg.BottomSurface = "Smooth"
    RightLeg.TopSurface = "Smooth"


    RHCA = CFrame.new(0.5, -1, 0) * CFrame.fromAxisAngle(Vector3.new(0, -1, 0), -math.pi/2)
    RHCB = CFrame.new(0, 1, 0) * CFrame.fromAxisAngle(Vector3.new(0, 1, 0), math.pi/2)
    RightHip = jointBetween(Torso, RightLeg, RHCA, RHCB)
    RightHip.Name = "Right Hip"
    RightHip.MaxVelocity = 0.1


    LeftArm = Instance.new("Part", game.Workspace)
    wait(0.1)
    LeftArm.Name = "Left Arm"
    LeftArm.FormFactor = "Symmetric"
    LeftArm.Anchored = false
    LeftArm.CanCollide = false
    LeftArm.Size = Vector3.new(1, 2, 1)
    LeftArm.CFrame = CFrame.new(1.5, 5.2, 4.5)
    LeftArm.Parent = parent
    LeftArm.BottomSurface = "Smooth"
    LeftArm.TopSurface = "Smooth"


    LSCA = CFrame.new(-1.0, 0.5, 0) * CFrame.fromAxisAngle(Vector3.new(0, 1, 0), -math.pi/2)
    LSCB = CFrame.new(0.5, 0.5, 0) * CFrame.fromAxisAngle(Vector3.new(0, 1, 0), -math.pi/2)
    LeftShoulder = jointBetween(Torso, LeftArm, LSCA, LSCB)
    LeftShoulder.Name = "Left Shoulder"
    LeftShoulder.MaxVelocity = 0.1


    RightArm = Instance.new("Part", game.Workspace)
    wait(0.1)
    RightArm.Name = "Right Arm"
    RightArm.FormFactor = "Symmetric"
    RightArm.Anchored = false
    RightArm.CanCollide = false
    RightArm.Size = Vector3.new(1, 2, 1)
    RightArm.CFrame = CFrame.new(-1.5, 5.2, 4.5)
    RightArm.Parent = parent
    RightArm.BottomSurface = "Smooth"
    RightArm.TopSurface = "Smooth"

    RSCA = CFrame.new(1.0, 0.5, 0) * CFrame.fromAxisAngle(Vector3.new(0, -1, 0), -math.pi/2)
    RSCB = CFrame.new(-0.5, 0.5, 0) * CFrame.fromAxisAngle(Vector3.new(0, 1, 0), math.pi/2)
    RightShoulder = jointBetween(Torso, RightArm, RSCA, RSCB)
    RightShoulder.Name = "Right Shoulder"
    RightShoulder.MaxVelocity = 0.1


    Head = Instance.new("Part", game.Workspace)
    wait(0.1)
    Head.Name = "Head"
    Head.FormFactor = "Symmetric"
    Head.Anchored = false
    Head.CanCollide = true
    Head.Size = Vector3.new(2, 1, 1)
    Head.CFrame = CFrame.new(0, 6.7, 4.5)
    Head.Parent = parent
    Head.BottomSurface = "Smooth"
    Head.TopSurface = "Smooth"

    Face = Instance.new("Decal", Head)
    Face.Name = 'face'
    Face.Texture = "rbxasset://textures/face.png"

    HeadM = Instance.new("SpecialMesh", Head)
    HeadM.MeshId = "rbxasset://fonts/head.mesh"

    NCA = CFrame.new(0, 1, 0, -1, 0, 0, 0, 0, 1, 0, 1, 0)
    NCB = CFrame.new(0, -0.5, 0, -1, 0, 0, 0, 0, 1, 0, 1, 0)
    Neck = jointBetween(Torso, Head, NCA, NCB)
    Neck.Name = "Neck"
    Neck.MaxVelocity = 0.1

    Humanoid = Instance.new("Humanoid", parent)
    Humanoid.MaxHealth = math.huge
    Humanoid.Health = math.huge

    Colors = Instance.new('BodyColors', parent)
    Colors.HeadColor = BrickColor.new('Bright yellow')
    Colors.LeftArmColor = BrickColor.new('Bright yellow')
    Colors.RightArmColor = BrickColor.new('Bright yellow')
    Colors.TorsoColor = BrickColor.new('Bright blue')
    Colors.LeftLegColor = BrickColor.new('Br. yellowish green')
    Colors.RightLegColor = BrickColor.new('Br. yellowish green')

    Root.Anchored = false
    TShirt = Instance.new("ShirtGraphic", parent)
    TShirt.Graphic = 'http://www.roblox.com/images/shirt.png'

    local root = game:GetService("InsertService"):LoadAsset(179547632)
    local instances = root:GetChildren()
    if #instances == 0 then
        root:Remove()
        return
    end
    for i = 1, #root:GetChildren() do
        root:GetChildren()[i].Parent = parent
    end

    wait(1)

    Humanoid.Jump = true

local debounce = false

Torso.Touched:connect(function(hit)
    if debounce == false then

        local ishuman = hit.Parent:FindFirstChild('Humanoid')

        if ishuman then
            debounce = true
            hit.Parent:BreakJoints()
            debounce = false
        else
            Humanoid.Jump = true
        end
    end
end)

Humanoid.Died:connect(function()
    wait(5)
    parent.Parent = nil
end)

function findNearestTorso(pos)
    local list = game.Workspace:children()
    local torso = nil
    local dist = 50
    local temp = nil
    local human = nil
    local temp2 = nil
    for x = 1, #list do
        temp2 = list[x]
        if (temp2.className == "Model") and (temp2 ~= parent) then
            temp = temp2:findFirstChild("Torso")
            human = temp2:findFirstChild("Humanoid")
            if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
                if (temp.Position - pos).magnitude < dist then
                    torso = temp
                    dist = (temp.Position - pos).magnitude
                end
            end
        end
    end
    return torso
end

while parent.Parent ~= nil and debounce == false do
    wait(0.1)
    local target = findNearestTorso(parent:WaitForChild('Torso').Position)
    if target ~= nil then
        parent.Humanoid:MoveTo(target.Position, target)
    end
end

I need someone to modify this so the humanoid safely regens when died, without breaking the functions. Can someone attempt this for me?

1 answer

Log in to vote
0
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
10 years ago

Well, I don't think anybody's going to read 261 lines of Lua, but let me give you some pointers. The first idea to came to mind was the Died event. I'm not sure if it'd work on NPC's, but since it's an event of the Humanoid, I'm pretty sure it will work. You could make a seperate script in the workspace, or possibly in the character, and when it dies, you could clone a model from ServerStorage and place it back into Workspace. Make sure to use the MakeJoints method. The other idea is to create a loop that would constantly check the health of the Humanoid, and when it is equal to or below 0, what I said above would happen.

0
Thank you. With that, now it properly regens for me. HECKFURNIOUS 85 — 10y
Ad

Answer this question