Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.