AI script is acting odd/broken

I’ve been playing around with AI lately so I tried making a little demo with creepers that blow up when you get close to them.
The only problem is that when more then creeper spawns things get weird like creeper not blowing up at all, creeper blows up at random, and they just don’t blow up at all

--local PATH_SERVICE = game:GetService("PathfindingService")
--local PATH = PATH_SERVICE:CreatePath({AgentRadius = 2, AgentHeight = 6,AgentCanJump = true});
local CURRENT_TARGET = nil;
local CREEPER = script.Parent.HumanoidRootPart;
local DEBRIS = game:GetService("Debris")
while wait() do
	--Select closet target
	for name,player in pairs(game.Players:GetPlayers()) do
		player.Character:WaitForChild("HumanoidRootPart")
		if CURRENT_TARGET == nil then
			CURRENT_TARGET = player;
		else
			if (CREEPER.Position - player.Character.HumanoidRootPart.Position).Magnitude < (CREEPER.Position - CURRENT_TARGET.Character.HumanoidRootPart.Position).Magnitude then
				CURRENT_TARGET = player;
			end
		end
	end
	CURRENT_TARGET.Character:WaitForChild("HumanoidRootPart")
		script.Parent.Humanoid:MoveTo(CURRENT_TARGET.Character.HumanoidRootPart.Position)
		if (CREEPER.Position - CURRENT_TARGET.Character.HumanoidRootPart.Position).Magnitude <= 3 then
			CREEPER.Sound:Pause()
			wait(3)
			local CREEPER = workspace.Creeper.HumanoidRootPart;
			local sound = Instance.new("Sound",CREEPER)
			sound.SoundId = "rbxassetid://5651511737";
			sound.SoundGroup = game.SoundService.SoundGroup;
			sound:Play()
			local bomb = Instance.new("Explosion",CREEPER)
			bomb.Position = CREEPER.Position
			bomb.ExplosionType = Enum.ExplosionType.Craters;
			wait(.8)
			sound:Destroy()
			CREEPER.Parent:Destroy()
		end
end

Sounds like you made Machine Learning these creepers are alive

I don’t think this will solve the situation completely but it may help. I see you are waiting for the humanoid root part of the target. What if it doesn’t load in or something and it just isn’t there? I would just check for the humanoidrootPart and if it’s there, then continue with letting it become the target. if player.Character:FindFirstChild(“HumanoidRootPart”). Waiting for a rootpart can cause a delay and mess up the creeper a bit. You would only have to do that once at the beginning of the for loop. The other WaitForChild isn’t necessary. I don’t think it will solve this issue but it will solve issues in the future.

I think I now see the issue. Why do you have it do
local CREEPER = workspace.Creeper.HumanoidRootPart;
when it explodes? If there are lets say 10 creepers in workspace, it’s just going to pick a random one to blow up. CREEPER is already set to the humanoidRootPart so unless there is something I’m missing, delete this line.

1 Like