Help with AI Damage Using Magnitude

Hey developers, I’m currently trying to make a Advanced Zombie AI Pathfinder using a module called “SimplePath” and well I got the pathfinding done but now all that’s left is damaging the player and well I am dumb on how to do that so any help is appreciated!

local SimplePath = require(script.SimplePath)

function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 100
	local temp = nil
	local human = nil
	local temp2 = nil
	local player = false
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("HumanoidRootPart")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if game.Players:GetPlayerFromCharacter(temp2) and (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
					
				end
			end
		end
	end
	return torso
end

local Dummy = script.Parent
local Goal = Vector3.new()
local Path = SimplePath.new(Dummy)

Path.Visualize = false

Path.Blocked:Connect(function()
	Path:Run(Goal)
end)

Path.WaypointReached:Connect(function()
	Path:Run(Goal)
end)

Path.Error:Connect(function(errorType)
	Path:Run(Goal)
end)

while true do
	wait(.2)
	local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
	if target then
		Goal = target.Position	
		--if Goal ~= nil then
			Path:Run(Goal)	
		else	
			Path:Stop()
			end
	end
--end

--Need help here, heres my very awful attempt--

local TargetTorso = findNearestTorso(script.Parent.HumanoidRootPart.Position)
if TargetTorso then
	if (TargetTorso.Position - script.Parent.HumanoidRootPart.Position).magnitude <= 10 then
		TargetTorso.Humanoid:TakeDamage(10)
	end
end

You had it right. The thing is, your code block will only execute once; you should perform this check in your while loop that handles moving the AI as well.

Try this out:

local target;

local function damageTarget()
if (script.Parent.PrimaryPart.Position - target.PrimaryPart.Position).Magnitude <= 10 then
target.Humanoid:TakeDamage(10)
end
end

while true do
	wait(.2)
        --//Make target a global variable instead
	target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
	if target then
--//Perform damage check
        damageTarget()
		Goal = target.Position	
		--if Goal ~= nil then
			Path:Run(Goal)	
		else	
			Path:Stop()
			end
	end
1 Like

Ummm it’s not working well… Here, to put it more simple and easy, i’ll give you the model down below and maybe that can be more easier for you to fix, thanks!

CHECK THE “MainAIScript” IN THE MODEL!
(edit: it already has the code you provided me inside of the MainAIScript)

Thank you for helping regardless C_Sharper

Another thong unrelated,but if you want to optimize as much as possible i recommend to loop through the player on player aervice instead as this will lag on model heavy place

Ooo good idea but I don’t think I have that much amount of IQ to do it, good suggestion!

(also replied for bump since I still need help)

Hmmm what help?I can guide you

You should merge the two loops into one, as suggested.

while true do
	wait(.2)
	local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
	if target then
		if (target.Position - script.Parent.HumanoidRootPart.Position).magnitude <= 10 then
			target.Humanoid:TakeDamage(10)
		end
		
		Goal = target.Position	
		--if Goal ~= nil then
		Path:Run(Goal)	
	else	
		Path:Stop()
	end
end
1 Like

Thanks for the solution! I realized I forgot to cover a question about how would I make it so that the AI can play a “swing animation” because currently I can’t do it or I have no clue it’s just not working for me

while true do
	wait(.5)
	local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
	if target then
		if (target.Position - script.Parent.HumanoidRootPart.Position).magnitude <= 5 then
			local randomAnimation = animationsFolder[math.random(1, #animationsFolder)]
			if not AnimTrack then
				AnimTrack = Humanoid:LoadAnimation(randomAnimation)
				AnimTrack.Priority = Enum.AnimationPriority.Action
				AnimTrack.Looped = false
			target.Parent.Humanoid:TakeDamage(10)
				PunchSFX:Play()
				AnimTrack:Play()
			
				end
		end
		
		Goal = target.Position	
		--if Goal ~= nil then
		Path:Run(Goal)	
	else	
		Path:Stop()
	end
end

image