Why does my npc keeps on breaking itself?

For some reason after my function finishes, the npc turns into parts. I think it has something to do with my weldconstraint.

local hum = script.Parent:WaitForChild("Humanoid")
local anim = hum:LoadAnimation(script.Parent.Eat)
while anim.Length <= 0 do task.wait() end

script.Parent.Hitbox.Touched:Connect(function(touch)
	if not touch.Parent:FindFirstChild("Humanoid") then return end
	
	local Char = touch.Parent
	
	if Char:FindFirstChild("Titan") then return end
	if not game.Players:FindFirstChild(Char.Name) then return end
	
	if script.Parent.InAttack.Value == false then
		script.Parent.InAttack.Value = true
		-- Char.HumanoidRootPart.Anchored = true
		Char.RightHand.CFrame = script.Parent.Hitbox.CFrame

		local weld = Instance.new("WeldConstraint")
		weld.Parent = Char
		weld.Part0 = Char.HumanoidRootPart
		weld.Part1 = script.Parent.Hitbox

		anim:Play()
		anim.Stopped:wait()
		weld:Destroy()
		Char.Humanoid.Health = 0
		script.Parent.InAttack.Value = false
	end
end)

Screen Shot 2021-11-09 at 2.10.36 PM

1 Like

local hum = script.Parent:WaitForChild(“Humanoid”)
local anim = hum:LoadAnimation(script.Parent.Eat)
while anim.Length <= 0 do task.wait() end

script.Parent.Hitbox.Touched:Connect(function(touch)
if not touch.Parent:FindFirstChild(“Humanoid”) then return end

local Char = touch.Parent

if Char:FindFirstChild("Titan") then return end
if not game.Players:FindFirstChild(Char.Name) then return end

if script.Parent.InAttack.Value == false then
	script.Parent.InAttack.Value = true
	-- Char.HumanoidRootPart.Anchored = true
	Char.RightHand.CFrame = script.Parent.Hitbox.CFrame

	local weld = Instance.new("WeldConstraint")
	weld.Parent = Char
	weld.Part0 = Char.HumanoidRootPart
	
end

end)

Where would I play the animation?

What is the main reason for this npc?

local hum = script.Parent:WaitForChild(“Humanoid”)
local anim = hum:LoadAnimation(script.Parent.Eat)
while anim.Length <= 0 do task.wait() end

script.Parent.Hitbox.Touched:Connect(function(touch)
if not touch.Parent:FindFirstChild(“Humanoid”) then return end

local Char = touch.Parent

if Char:FindFirstChild("Titan") then return end
if not game.Players:FindFirstChild(Char.Name) then return end

if script.Parent.InAttack.Value == false then
	script.Parent.InAttack.Value = true
	-- Char.HumanoidRootPart.Anchored = true
	Char.RightHand.CFrame = script.Parent.Hitbox.CFrame

	local weld = Instance.new("WeldConstraint")
	weld.Parent = Char
	weld.Part0 = Char.HumanoidRootPart
	weld.Part1 = script.Parent.Hitbox

	anim:Play()
	anim.Stopped:wait()
	script.Parent.InAttack.Value = false
end

end)

It is a npc which attacks people

weld:Destroy()

You’re destroying the weld here.

Screen Shot 2021-11-09 at 2.27.07 PM

Still the same problem.

local hum = script.Parent:WaitForChild(“Humanoid”)
local anim = hum:LoadAnimation(script.Parent.Eat)
while anim.Length <= 0 do task.wait() end

script.Parent.Hitbox.Touched:Connect(function(touch)
if not touch.Parent:FindFirstChild(“Humanoid”) then return end

local Char = touch.Parent

if Char:FindFirstChild("Titan") then return end
if not game.Players:FindFirstChild(Char.Name) then return end

if script.Parent.InAttack.Value == false then
	script.Parent.InAttack.Value = true
	-- Char.HumanoidRootPart.Anchored = true
	Char.RightHand.CFrame = script.Parent.Hitbox.CFrame

	local weld = Instance.new("WeldConstraint")
	weld.Parent = Char
	weld.Part0 = Char.HumanoidRootPart
	weld.Part1 = script.Parent.Hitbox

	anim:Play()
	anim.Stopped:wait()
	weld:Destroy() Script.Parent.Humanoid.MaxHealth(onTouched) = 0 

Script.Parent.Humanoid.Health(onTouched) = 0

script.Parent.InAttack.Value = false
end
end)

What does the onTouched do? I don’t have a onTouched function.

You litterally changed the NPC’s health to 0. Thats why it’s dying. Remove this line its at the end of the script.

Char.Humanoid.Health = 0

I would like to clarify that Char is the player the npc is attacking, not the npc itself. Now all of a sudden the npc isn’t breaking without doing anything which is weird. I believe it might have something to do with it tho.

local hum = script.Parent:WaitForChild("Humanoid")
local anim = hum:LoadAnimation(script.Parent:WaitForChild("Eat"))
while anim.Length <= 0 do task.wait() end
local hitbox = script.Parent:WaitForChild("Hitbox")
local inAttack = script.Parent:WaitForChild("InAttack")

hitbox.Touched:Connect(function(touch)
	if touch.Parent:FindFirstChild("Accessory") then
		local character = touch.Parent
		if character:FindFirstChild("Titan") then
			return
		end
		
		if not inAttack.Value then
			inAttack.Value = true
			character:WaitForChild("RightHand").CFrame = hitbox.CFrame

			local weld = Instance.new("WeldConstraint")
			weld.Parent = workspace
			weld.Part0 = character:WaitForChild("HumanoidRootPart")
			weld.Part1 = hitbox
			anim:Play()
			anim.Stopped:Wait()
			weld:Destroy()
			character.Humanoid.Health = 0
			task.wait(1)
			inAttack.Value = false
		end
	end
end)

Make sure the NPC isn’t triggering its own “Touched” event since it will also have a child instance named “Humanoid”. Use a player unique instance instead, “Accessory” for example.

1 Like