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

How do I make this NPC retreat when low on health?

Asked by 5 years ago
Edited 5 years ago

I'm trying to make an advanced SCP-106 from scp, and to do this I want him to retreat to his pocket dimension if his health is low enough. But I ran into some issues.

  • Doesn't respond to low health.
  • When health is low enough (I set his humanoid health) he is anchored.

I'm not sure if these are the only issues since I can't test it further.

local hum = script.Parent.Humanoid
local health = hum.Health
local dim = workspace.PocketDimension
local tele = workspace.PocketDimension.HumanoidRootPart
local humroot = hum.Parent.HumanoidRootPart

local sink = hum.Parent.Head.Sink
local kneel = hum.Parent.Head.Kneel


if hum.Health <= 25 then
    sink:Play()
    humroot.Anchored = true
    for i = 1,200 do
        humroot.CFrame = CFrame.new(humroot.Position-.05)
        wait() 
    end
    wait(7.7)
    humroot.CFrame = CFrame.new(workspace.PocketDimension.HumanoidRootPart.Position) 
    humroot.Anchored = false
    kneel:play()
if hum.Health >= 26 then
     hum.Parent.Parent.Parent.Parent.OLDMANARRIVE.Arrive:Play()
hum.Parent.Parent.Parent.Parent.OLDMANARRIVE.SurfaceLight.Enabled = true
        for i =1,20 do
             hum.Parent.Parent.Parent.Parent.OLDMANARRIVE.Decal.Transparency = script.Parent.Parent.OLDMANARRIVE.Decal.Transparency - .05
            wait(.05)
        end
        humroot.CFrame = CFrame.new(hum.Parent.Parent.Parent.Parent.HumanoidRootPart.Position) 
        wait(.2)
            wait(10)
        for i=1,20 do
            wait(.05)
            hum.Parent.Parent.Parent.Parent.OLDMANARRIVE.Decal.Transparency = script.Parent.Parent.OLDMANARRIVE.Decal.Transparency + .05
                hum.Parent.Parent.Parent.Parent.OLDMANARRIVE.SurfaceLight.Enabled = false
        end
end
end

UPDATED SCRIPT:

*I fixed Y-Axis movement, but it still doesn't work correctly.

local hum = script.Parent.Humanoid
local health = hum.Health
local dim = workspace.PocketDimension
local tele = workspace.PocketDimension.HumanoidRootPart
local humroot = hum.Parent.HumanoidRootPart

local sink = hum.Parent.Head.Sink
local kneel = hum.Parent.Head.Kneel

hum.HealthChanged:Connect(function()
    if hum.Health <= 25 then
        sink:Play()
        humroot.Anchored = true
        for i = 1,200 do
            humroot.CFrame = CFrame.new(humroot.Position.Y-.5)
            wait() 
        end
        wait(7.7)
        humroot.CFrame = CFrame.new(workspace.PocketDimension.HumanoidRootPart.Position) 
        humroot.Anchored = false
        kneel:play()
    if hum.Health >= 26 then
         hum.Parent.Parent.Parent.Parent.OLDMANARRIVE.Arrive:Play()
    hum.Parent.Parent.Parent.Parent.OLDMANARRIVE.SurfaceLight.Enabled = true
            for i =1,20 do
                 hum.Parent.Parent.Parent.Parent.OLDMANARRIVE.Decal.Transparency = script.Parent.Parent.OLDMANARRIVE.Decal.Transparency - .05
                wait(.05)
            end
            humroot.CFrame = CFrame.new(hum.Parent.Parent.Parent.Parent.HumanoidRootPart.Position) 
            wait(.2)
                wait(10)
            for i=1,20 do
                wait(.05)
                hum.Parent.Parent.Parent.Parent.OLDMANARRIVE.Decal.Transparency = script.Parent.Parent.OLDMANARRIVE.Decal.Transparency + .05
                    hum.Parent.Parent.Parent.Parent.OLDMANARRIVE.SurfaceLight.Enabled = false
            end
    end
    end
end)

1 answer

Log in to vote
0
Answered by
hellmatic 1523 Moderation Voter
5 years ago

Your code only runs once. Wrap it inside a HealthChanged event

local hum = script.Parent.Humanoid
local health = hum.Health
local dim = workspace.PocketDimension
local tele = workspace.PocketDimension.HumanoidRootPart
local humroot = hum.Parent.HumanoidRootPart

local sink = hum.Parent.Head.Sink
local kneel = hum.Parent.Head.Kneel

hum.HealthChanged:Connect(function()
    if hum.Health <= 25 then
        sink:Play()
        humroot.Anchored = true
        for i = 1,200 do
            humroot.CFrame = CFrame.new(humroot.Position-.05)
            wait() 
        end
        wait(7.7)
        humroot.CFrame = CFrame.new(workspace.PocketDimension.HumanoidRootPart.Position) 
        humroot.Anchored = false
        kneel:play()
    if hum.Health >= 26 then
         hum.Parent.Parent.Parent.Parent.OLDMANARRIVE.Arrive:Play()
    hum.Parent.Parent.Parent.Parent.OLDMANARRIVE.SurfaceLight.Enabled = true
            for i =1,20 do
                 hum.Parent.Parent.Parent.Parent.OLDMANARRIVE.Decal.Transparency = script.Parent.Parent.OLDMANARRIVE.Decal.Transparency - .05
                wait(.05)
            end
            humroot.CFrame = CFrame.new(hum.Parent.Parent.Parent.Parent.HumanoidRootPart.Position) 
            wait(.2)
                wait(10)
            for i=1,20 do
                wait(.05)
                hum.Parent.Parent.Parent.Parent.OLDMANARRIVE.Decal.Transparency = script.Parent.Parent.OLDMANARRIVE.Decal.Transparency + .05
                    hum.Parent.Parent.Parent.Parent.OLDMANARRIVE.SurfaceLight.Enabled = false
            end
    end
    end
end)
Ad

Answer this question