AI script Not working

Hey Everyone, I’m trying to make my monster teleport more than once, currently it only teleports once, and then chases the same player. I need it to teleport to a random player.
Here is my current code:

local Monster = script.Parent
local humanoid = Monster.Humanoid
local PlayerService = game:GetService('Players')

function FindTarget()
	local ChosenPlayer = game.Players:GetChildren()[math.random(1, #game.Players:GetChildren())]
	if ChosenPlayer.Character.Humanoid.Health > 0 then
		Monster.TeleportSpirit.Fire.Enabled = true
		wait(1.5)
		Monster.HumanoidRootPart.CFrame = ChosenPlayer.Character.HumanoidRootPart.CFrame - Vector3.new(0,0,20)
		wait(1.5)
		Monster.TeleportSpirit.Fire.Enabled = false
		repeat
			humanoid:MoveTo(ChosenPlayer.Character.HumanoidRootPart.Position)
			wait()
		until humanoid.Health == 0
	else
		return
	end
end

while true do
	wait(5)
	FindTarget()
end


Here is what I mean: https://gyazo.com/c5d683451f1c2e1ac76156ca3dbd0963

Thanks for any help!

You didn’t add a wait in your repeat loop

1 Like

Should I do this then?

		repeat humanoid:MoveTo(ChosenPlayer.Character.HumanoidRootPart.Position) wait()
			
		until humanoid.Health == 0
1 Like

Yes, or you could format the repeat block better like this

repeat
	humanoid:MoveTo(ChosenPlayer.Character.HumanoidRootPart.Position)
	wait()
until humanoid.Health == 0
1 Like

Also, why does the monster only teleport once? There are no errors. It’s supposed to teleport every 5 seconds that’s why I’m using a while loop.

It’s supposed to teleport to a new random player every 5 seconds and attack them.

1 Like

I’m not exactly sure because I don’t work with Characters/Humanoids a lot. I’m guessing you’re not supposed to run the FindTarget function multiple times.

Try doing this

local Monster = script.Parent
local humanoid = Monster.Humanoid
local PlayerService = game:GetService('Players')

while true do
    local ChosenPlayer = game.Players:GetChildren()[math.random(1, #game.Players:GetChildren())]
	if ChosenPlayer.Character.Humanoid.Health > 0 then
		Monster.TeleportSpirit.Fire.Enabled = true
		wait(1.5)
		Monster.HumanoidRootPart.CFrame = ChosenPlayer.Character.HumanoidRootPart.CFrame - Vector3.new(0,0,20)
		wait(1.5)
		Monster.TeleportSpirit.Fire.Enabled = false
		repeat humanoid:MoveTo(ChosenPlayer.Character.HumanoidRootPart.Position)
			
		until humanoid.Health == 0
	else
		return
	end
    wait(5)
end
1 Like

That didn’t work, the monster only teleports once.

1 Like

I’ve edited my post with the new issue.

I managed to fix it:

local Monster = script.Parent
local humanoid = Monster.Humanoid
local PlayerService = game:GetService('Players')

function FindTarget()
	local ChosenPlayer = game.Players:GetChildren()[math.random(1, #game.Players:GetChildren())]
	if ChosenPlayer.Character.Humanoid.Health > 0 then
		print("Looping")
		wait(1.5)
		Monster.HumanoidRootPart.CFrame = ChosenPlayer.Character.HumanoidRootPart.CFrame - Vector3.new(0,0,20)
		wait(1.5)
		
		while ChosenPlayer.Character.Humanoid.Health > 0 do
			if ChosenPlayer.Character.Humanoid.Health == 0 then break end
			humanoid:MoveTo(ChosenPlayer.Character.HumanoidRootPart.Position)
			wait()
		end
	else
		print("Ending")
		return
	end
end

while true do
	wait(5)
	FindTarget()
end

The repeat loop you had kept running so it interrupted the main loop. Also I think you were supposed to use ChosenPlayer.Character.Humanoid.Health instead of humanoid.Health

Note: I removed the fire effects while I was testing, so yeah :no_mouth:

2 Likes

Um, It’s not fixed. I don’t think you understand. It’s supposed to teleport every 5 seconds without needing to kill them, so even if it doesn’t kill them it will teleport to someone else after 5 seconds.

Basically, I need it so the monster randomly teleports to someone every 5 seconds, just like this: https://gyazo.com/c5d683451f1c2e1ac76156ca3dbd0963

As you can see, it doesn’t teleport every time it kills the player.
I need it to teleport to a certain player, and move towards them, and after 5 seconds it should run the function again. But it doesn’t teleport again, only once.

Attempt #2 (this one most likely works)

local Monster = script.Parent
local humanoid = Monster.Humanoid
local PlayerService = game:GetService('Players')

local TrackingStatus = false

local function GetNewPlayer()
	print("Getting New Player")
	return PlayerService:GetPlayers()[math.random(1, #PlayerService:GetPlayers())]
end

local function TrackNewPlayer(Target)
	print("Tracking New Target")
	Monster.HumanoidRootPart.CFrame = Target.HumanoidRootPart.CFrame - Vector3.new(0,0,20)
	local function Track()
		while TrackingStatus do
			if not TrackingStatus then break end
			if Target.Humanoid.Health == 0 then break end
			humanoid:MoveTo(Target.HumanoidRootPart.Position)
			wait()
		end
	end
	coroutine.resume(coroutine.create(Track)) -- don't call me out for this :P
end

while true do
	print("AI Running")
	while #PlayerService:GetPlayers() == 0 do wait() end
	local Target = GetNewPlayer()
	local Character = Target.Character
	if Character then
		if Character.Humanoid.Health > 0 then
			TrackingStatus = true
			TrackNewPlayer(Character)
			wait(5)
			TrackingStatus = false
			print("Tracking Ended")
		end
	end
	wait()
end

Pretty much your code, I just divided it into more parts.

2 Likes