I have tried to execute something similar to what's in 'Demented Defense'. The AI is originally headed towards the Castles Hit Point when spawned, and when a player is near, it will go after he or she. If the player is killed, it should return back on it's path to the Castles Hit Point. When I test in studio, the above works perfectly fine. However when I am online to test it, the AI stands in one spot not knowing what to do after the player that they were chasing has died.

Please help.

local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")

function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 50
	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) and (temp2.Name ~= "Enemies") and (temp2.Name ~= "boss_import") and (temp2.Name ~= "castle_build") and (temp2.Name ~= "game_portal_1") and (temp2.Name ~= "game_portal_2") and (temp2.Name ~= "game_portal_3") and (temp2.Name ~= "game_portal_4") and (temp2.Name ~= "Gate1 Level 2") and (temp2.Name ~= "Gate1 Level 1") and (temp2.Name ~= "Gate2 Level 2") and (temp2.Name ~= "Gate1 Level 3") and (temp2.Name ~= "Gate2 Level 3") and (temp2.Name ~= "Gate3 Level 3") and (temp2.Name ~= "Gate1 Level 4") and (temp2.Name ~= "Gate2 Level 4") and (temp2.Name ~= "Gate3 Level 4") and (temp2.Name ~= "Soul Berzerker") and (temp2.Name ~= "Soul Guest") and (temp2.Name ~= "Soul Noob") and (temp2.Name ~= "Soul Ravanger") and (temp2.Name ~= "Soul Warrior") and (temp2.Name ~= "wall_castle_boundaries") then
			temp = temp2:findFirstChild("Torso")
			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
			elseif (temp ~= nil) and (human ~= nil) and (human.Health == 0) then
				
			end
		end
	end
	return torso
end

while true do
	wait(0.1)
	local target = findNearestTorso(script.Parent.Torso.Position)
	if target ~= nil then
		script.Parent.Humanoid:MoveTo(target.Position, target)
	elseif target == nil then
		script.Parent.Humanoid:MoveTo(game.Workspace.castle_hitpoint.Position)
	end
end

8 comments

0

Oh Christ.

0

You got it queen

0

I'm sorry for the big bunch, I really need help with this honestly.

0

Why aren't you just looping through game.Players and using player.Character for line 13?

0

I can change that later, I need to figure why it won't go back towards the castle after the players death.

0

And you need "game.Workspace:GetChildren()" on line 5. Or even better "game.Players:GetPlayers()".

0

Hmm, I'll try that. I will let you know the results.

0

Hmm, I'll try that. I will let you know the results.