Healthbar GUI for NPC that uses magnitude not resetting as the NPC dies

Process of Healthbar GUI:

  • TweenPosition as Enter on a client screen when the client’s head magnitude and NPC’s head magnitude are on the same declared magnitude distance.

  • TweenPosition as Exit on a client screen when the client’s head magnitude and NPC’s head magnitude are on the same declared magnitude distance.

My Problem:

  • When the NPC dies the Healthbar GUI should reset and do the TweenPosition as Exit because the NPC’s head is destroyed as the NPC is respawned the magnitude distance that activates the Tweening should reset too but it is not working.

The LocalScript:

local monster = workspace:WaitForChild("Killers"):WaitForChild("Evil Doctor")
local magDistance = 50
local frame = script.Parent:WaitForChild("HPFrame")

frame.Position = UDim2.new(0.5, 0,-1, 0)


while wait() do
	local magnitude = (monster.Head.Position - game.Players.LocalPlayer.Character.Head.Position).Magnitude
	
	if magnitude <= magDistance then
	    frame:TweenPosition(UDim2.new(0.5,0,0.046,0), "InOut", "Linear", 0.5)
	else
		frame:TweenPosition(UDim2.new(0.5,0,-1,0), "InOut", "Linear", 0.5)
	end
end

Also:

  • Should I use a While Loop for this script? (Sorry Im new in Lua)
2 Likes