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

[UNSOLVED] How can/should I go about fixing this AI targetting damage issue?

Hello there, i'm having spots of trouble with an AI functionality that i've stringed together for my custom Spider mob.

The core functionality is working just fine, / mob starts chasing its target when its in the correct range and then when its in an even closer range, it'll start an attack animation and then the script fires a KeyframeReached function to check if the target is in hitting distance and if it is then it'll dish out damage to the target.

The problem is immediately apparent when there is more than one target, if the spider, chooses to target Player1 and then Player1 were to run away from it out of range and then Player2 were to run up to it and trigger its chasing function, everytime the spider attacks Player2, only Player1 would recieve the damage done by the Spider until either Player1 dies and respawns or the spider does.

Here is the code from the script that i'm currently using to control the AI:

local larm = script.Parent:FindFirstChild("HumanoidRootPart") 
local rarm = script.Parent:FindFirstChild("HumanoidRootPart") 
local spiderhum = script.Parent:WaitForChild("Humanoid")
local spiderattack = spiderhum:LoadAnimation(script.AttackAnim)



function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 75
	local temp = nil
	local human = nil
	local temp2 = nil
	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 (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end




while true do
	wait(math.random(1,5))
	local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
	if target ~= nil then
		
		script.Parent.Humanoid:MoveTo(target.Position, target)
		if (target.Position - script.Parent.Head.Position).magnitude <10 then 
			
			wait (2)
			spiderattack:Play()
			spiderattack.KeyframeReached:connect(function(Damage)
				target.Parent.Humanoid:TakeDamage(20)
				
			end)
		end 
	end

end

If I were to make a guess it would be that it's a debounce or two that needs implementing but its just a matter of where and when, but thats only a guess and I'm still uncertain

i'm not incredibly well versed in scripting and honestly i've only just began scripting properly within the last year or so I do apologise if the code is somewhat hard to read, I'm happy to clarify anything that needs clarifying so hopefully the end result can be a bug free spider excuse the pun.

Thankyou for any help or shoves into the right direction you may be able to provide and have yourselves a great day :^)

1 answer

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