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

enemy NPC spams walking and idle after there are no unit NPC in the field, how do i fix this?

This was pretty painful to fix though i wont get answers in this website and i still need more activities reading to get post in devforum..

Though ill send the video and script to see what hell is happening https://streamable.com/gifvbq


local npc = script.Parent
local global = require(game.Workspace.Enviroment.GlobalVar)

local baseunit = game.Workspace.Enviroment["Base-unit"]
local baseenemy = game.Workspace.Enviroment["Base-Enemy"]

local part = Instance.new("Part")
local weld = Instance.new("WeldConstraint")

weld.Part0 = npc:WaitForChild("HumanoidRootPart")
weld.Part1 = part; weld.Parent = part

part.Name = 'Hitbox'
part.Anchored = false; part.CanCollide = true
part.Massless = true
part.CanCollide = false
part.Transparency = 0.5
part.Size = Vector3.new(3.2, 5.8, 3.8)
part.CFrame = (npc.HumanoidRootPart.CFrame + Vector3.new(0, 0, -part.Size.Z *0.5))
part.Parent = npc

local animation = {
	idle = npc.Idle,
	run = npc.Run,
	atk = npc.Attack
}


local IdleTrack = npc.Humanoid:LoadAnimation(animation.idle)
local AttackTrack = npc.Humanoid:LoadAnimation(animation.atk)
local RunTrack = npc.Humanoid:LoadAnimation(animation.run)

--RunTrack.Looped = true
--IdleTrack.Looped = true

local animtable = {
	['idle'] = IdleTrack,
	['run'] = RunTrack,
	['atk'] = AttackTrack
}

local animstring = 'run'

local pathfinding_service = game:GetService("PathfindingService")

local timer = nil
local idleTimer = 0
local timeAfterAtk = 1.4

local isOn = false

function HasChild(Name, Model)
	for _, Child in pairs(Model:GetChildren()) do
		if Child.Name == Name then
			return true
		end
	end
	return false
end

function onHit()
	if (timer ~= nil and timer <= 0) and isOn then
		for i, entity in pairs(game.Workspace.Unit:GetChildren()) do
			if game.Workspace.Unit:FindFirstChild(entity.Name) then
				local relativePos
				if HasChild('HumanoidRootPart',entity) then
					relativePos = npc.Hitbox.CFrame:pointToObjectSpace(entity:WaitForChild("HumanoidRootPart").Position)
				end
				if HasChild('HumanoidRootPart',entity) and math.abs(relativePos.X) <= npc.Hitbox.Size.X * 1 * 0.5 and math.abs(relativePos.Z) <= npc.Hitbox.Size.Z * 1 * 0.5 then
					entity.Humanoid:TakeDamage(55)
					print(tostring(entity.Name)..' HIT!')
				else
					print(tostring(entity.Name)..' Dodged!')
				end
			end
		end
		timer = nil
		isOn = false
		idleTimer = timeAfterAtk
	end
end

local curWalk = npc.Humanoid.WalkSpeed

isOn2 = true

function lerp(a, b, t)
	return a + (b - a) * t
end

while wait() do
	if npc.Humanoid.Health <= 0 then
		task.wait(2)
		for i,v in pairs(npc:GetChildren()) do
			if v:IsA("Part") or v:IsA('MeshPart') then
				npc:FindFirstChild(v.Name).CFrame = CFrame.new(Vector3.new(0,-20,0))
			end
		end 
		break
	end

	if AttackTrack.IsPlaying or IdleTrack.IsPlaying or idleTimer >= 0 then
		npc.Humanoid.WalkSpeed = 0
		print('is my reddummy man idling??')
	elseif idleTimer <= 0 then
		npc.Humanoid.WalkSpeed = curWalk
		print('is my reddummy man walking??')
	end
	if not animtable[animstring].isPlaying then
		for i,v in pairs(animtable) do	
			animtable[i]:Stop()
		end
		animtable[animstring]:Play()
	end
	
	npc.Humanoid:MoveTo(baseunit.Position + Vector3.new(8,0,0))

	for i, entity in pairs(game.Workspace.Unit:GetChildren()) do
		if idleTimer < 0 and (npc:FindFirstChild('HumanoidRootPart').Velocity.Magnitude) >=0.004 and not isOn then
			animstring = 'run'
		else
			animstring = 'idle'
		end
		if game.Workspace.Unit:WaitForChild(entity.Name) then
			local hitbox = npc:WaitForChild('Hitbox')
			local relativePos
			if HasChild('HumanoidRootPart',entity) then
				relativePos = hitbox.CFrame:pointToObjectSpace(entity:WaitForChild("HumanoidRootPart").Position)
			end
			if idleTimer <= 0 and entity:WaitForChild("Humanoid").Health > 0 and math.abs(relativePos.X) <= hitbox.Size.X * 0.8 * 0.5 and math.abs(relativePos.Z) <= hitbox.Size.Z * 0.8 * 0.5 then
				--print(tostring(entity.Name).." is inside")
				animstring = 'atk'
				if not isOn then
					timer = 0.8
				end
				isOn = true
			elseif idleTimer > 0 and entity:WaitForChild("Humanoid").Health > 0 and math.abs(relativePos.X) <= hitbox.Size.X * 0.8 * 0.5 and math.abs(relativePos.Z) <= hitbox.Size.Z * 0.8 * 0.5 then
				animstring = 'idle'
			end
		end
	end
	onHit()
	if timer ~= nil and timer >= 0 then
		timer -= 0.015
	end
	if idleTimer >= 0 then
		idleTimer -= 0.015
		--print(idleTimer)
	end
end

1 answer

hey you! have you ever heard of enes? if you are in trouble, better call enes!

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