Problems with AI

I am currently making a tower defense game but I have a few problems that have presented themselves to me.

First problem being, when I test in game the AI will sometimes randomly fall over after spawning:

It works perfectly fine in studio and I am not sure what the problem is with them.

My 2nd problem is that the AI in game and in studio are extremely choppy when running and will freeze for about 0.1 seconds when they die. I run all their movement on server and I would really like to keep it that way but the lag is just awful, if anyone has any ideas please let me know, thanks.

2 Likes

I had a memory leak issue with one of my games because all of the NPCs I spawned just had the usual ‘Animation’ script cloned in them. When the NPCs were destroyed, the script continued to use up memory and overtime it created a lot of lag. So my first question is, how are you handling your NPCs animations?

It could possibly be the collision with the part when it spawns in due to the animation, does it spawn slightly over the part and is the animation slightly delayed?

The animation is loaded and played as soon as they spawn and deleted as soon as they are destroyed
Here is an example of my code:

_G.AI = function()
local Dummy = ServerStorage:FindFirstChild("Dummy"):Clone()
local Humanoid = Dummy:FindFirstChild("Humanoid")
Dummy.Parent = Workspace
table.insert(_G.CurrentAI, Dummy)
wait(0.1)
Humanoid.Died:Connect(function()
	spawn(function()
		wait(0.5)
		Dummy:Destroy()
		for i,v in pairs(_G.CurrentAI) do
			if v == Dummy then
				table.remove(_G.CurrentAI, i)
				break
			end
		end
	end)
end)
local AnimationTrack = Humanoid:LoadAnimation(RunAnimation)
AnimationTrack:Play()
local Distance
for i = 1, #_G.Nodes do
	local WaypointPosition = _G.Nodes[i]
	Humanoid:MoveTo(WaypointPosition)
	Humanoid.MoveToFinished:Wait()
end
AnimationTrack:Stop()
AnimationTrack:Destroy()
for i,v in pairs(_G.CurrentAI) do
	if v == Dummy then
		table.remove(_G.CurrentAI, i)
		break
	end
end
Dummy:Destroy()

end

That is definitely a possibility but it would not explain why it only happens in game and not in studio

From my experience, typically Studio can run a little bit smoother than the actual game, a lot of things are different in Studio than the actual game. It’s probably worth a try.

Alright I will try spawning them above the part now

Didn’t seem to work they still fell over

I’m not sure if this is possible, but can you use LoadAnimation before parenting the Dummy? That might help a little bit. I think maybe also using a dictionary instead of a table to easily add and remove Dummies from your _G.CurrentAI table would help.

I’ll try the animation loading thing but I prefer to use the table because I like the AI to be ordered in the way they spawn so it’s easier for my turrets to prioritize what to shoot

Yea trying to :LoadAnimation() before it’s parent is Workspace just errors

:ChangeState(10)
Solution to the problem thanks for the help guys

2 Likes