Enemy Movement System not working correctly

Gnome Code Doesnt help bc i Use CFrame to move my Enemys instead of Humanoid:MoveTo (wich is bad)

Script in the ServerScriptService i call it EnemyHandler

-- this is just a script to test everything nothing finished
local waypoints = workspace.Waypoints
local replicatedStorage = game:GetService("ReplicatedStorage")
local enemy = replicatedStorage.Enemy
local EnemyFuncs = require(script.Enemy)
game.Players.PlayerAdded:Connect(function(player)
	for count = 10, 0, -1 do
		if count == 30 then
			warn("Game Starts in 30 Seconds")
		elseif count == 20 then
			warn("Game Starts in 20 Seconds")
		elseif count == 10 then
			warn("Game Starts in 10 Seconds")
		elseif count < 10 and count > 0 then
			warn(count)
		end
		task.wait(1)
	end
	warn("Game Started")
	EnemyFuncs.Spawn(enemy, 5, 0.5)
end)

Modul Script (child of EnemyHandler) i call it Enemy

local enemyFuncs = {}

local waypoints = workspace.Waypoints
local sp = workspace.Enemyspawn

function enemyFuncs:Move()
	local waypointIndex = 1
	local connection 
	
	connection = game:GetService("RunService").Heartbeat:Connect(function()
		if waypointIndex <= #waypoints:GetChildren() then
			local tP = waypoints:GetChildren()[waypointIndex].Position
			local direction = (tP - self.HumanoidRootPart.Position).Unit
			local orientation = CFrame.new(self.HumanoidRootPart.Position, tP)
			
			warn("Current Position: ", self.HumanoidRootPart.Position)
			warn("Target Position: ", tP)

			self.HumanoidRootPart.Position = self.HumanoidRootPart.Position + direction * self.Speed.Value * (1/60)
			self.HumanoidRootPart.CFrame = orientation

			if (tP - self.HumanoidRootPart.Position).Magnitude < 0.1 then
				waypointIndex += 1
			end
		else
			connection:Disconnect()
			self:Destroy()
		end
	end)
end

function enemyFuncs:Spawn(amount, spawnInterval)
	
	for count = 1, amount, 1 do
		local enemy = self:Clone()
		enemy.Parent = workspace.Enemys
		enemy.HumanoidRootPart.CFrame = workspace.Enemyspawn.CFrame
		spawn(function()
			enemyFuncs.Move(enemy)
		end)
		task.wait(spawnInterval)
	end
	
	warn("Enemy's Spawned")

end

return enemyFuncs

so what is my Problem, Actualy my problem is, the enemys are not walking. they are not moving at all, all they do is bugging around and moving buggy around like they are glitched in the ground. after a Curely time they Glitch in the air and stay there for no reason… did i do something wrong? i dont think so. the Waypoints are Placed above the Path. so the enemys are spawning above the path as well

I hope someone could help

Why are you not using :MoveTo() on the humanoid? If you need to use RunService, I would restructure this:

local event = game:GetService("RunService").Heartbeat
local waypointIndex = 1
function enemyFuncs:NextPoint()
	waypointIndex += 1
	if waypointIndex <= #waypoints:GetChildren() then
		local tP = waypoints[waypointIndex].Position
		local direction = (tP - self.HumanoidRootPart.Position).Unit
		local orientation = CFrame.new(self.HumanoidRootPart.Position, tP)
			
		warn("Current Position: ", self.HumanoidRootPart.Position)
		warn("Target Position: ", tP)

		self.HumanoidRootPart.Position = self.HumanoidRootPart.Position + direction * self.Speed.Value * (1/60)
		self.HumanoidRootPart.CFrame = orientation
		connection = event:Connect(function()
			if (tP - self.HumanoidRootPart.Position).Magnitude < 0.1 then
				connection:Disconnect()
				enemyFuncs.NextPoint(self)
			end
		end)
	else
		self:Destroy()
	end
end
function enemyFuncs:Move()
	local connection 
	
	connection = game:GetService("RunService").Heartbeat:Connect(function()
		if waypointIndex <= #waypoints:GetChildren() then
			
			if (tP - self.HumanoidRootPart.Position).Magnitude < 0.1 then
				waypointIndex += 1
			end
		else
			connection:Disconnect()
			self:Destroy()
		end
	end)
end

So I made a new NextPoint method that will go to the next point. Your code would update the position every frame and had some weird methods. You also indexed the GetChildren() list, which won’t be in order. Make sure your waypoints are all ordered.

I see a few typos in the second script that can use some fixing so here’s a updated one on the second!

local enemyFuncs = {}

local waypoints = workspace.Waypoints
local sp = workspace.Enemyspawn

function enemyFuncs:Move()
	local waypointIndex = 1
	local connection 
	
	connection = game:GetService("RunService").Heartbeat:Connect(function()
		if waypointIndex <= #waypoints:GetChildren() then
			local tP = waypoints:GetChildren()[waypointIndex].Position
			local direction = (tP - self.HumanoidRootPart.Position).Unit
			local orientation = CFrame.new(self.HumanoidRootPart.Position, tP)
			
			warn("Current Position: ", self.HumanoidRootPart.Position)
			warn("Target Position: ", tP)

			self.HumanoidRootPart.Position = self.HumanoidRootPart.Position + direction * self.Speed.Value * (1/60)
			self.HumanoidRootPart.CFrame = orientation

			if (tP - self.HumanoidRootPart.Position).Magnitude < 0.1 then
				waypointIndex += 1
			end
		else
			connection:Disconnect()
			self:Destroy()
		end
	end)
end

function enemyFuncs:Spawn(amount, spawnInterval)
	
	for count = 1, amount, 1 do
		local enemy = self:Clone()
		enemy.Parent = workspace.Enemys
		enemy.HumanoidRootPart.CFrame = workspace.Enemyspawn.CFrame
		spawn(function()
			enemyFuncs.Move(enemy)
		end)
		task.wait(spawnInterval)
	end
	
	warn("Enemy's Spawned")

end

return enemyFuncs

THE TYPOS:

  1. The script is missing the definition of the self variable. In order to use self as a reference to the enemy object, you need to define it as a parameter in the functions or use a different approach.

  2. The waypointIndex += 1 line is not valid syntax. In Lua, you need to use the waypointIndex = waypointIndex + 1 syntax to increment the variable.

  3. The self:Clone() line is referencing a method that is not defined in the script. Make sure you have a Clone method defined for the enemy object.

  4. The task.wait(spawnInterval) line is using an incorrect syntax. It should be wait(spawnInterval) instead.

Please review the changes and adjust them according to your specific needs, this way you can have a nice system! Hope this helps in some way. Good script though!

I would really appreciate it if you wouldn’t use ChatGPT, outdated sources, or just try to correct things while possessing limited knowledge. All of the changes you listed here are incorrect.

  1. The self keyword is automatically obtained when declaring a method with a colon. You can then pass in an instance as the first argument and it will automatically be assigned to self. ObjectOrientedProgramming is an advanced concept, so it is understandable if you don’t have a ton of experience with it.
  2. I understand if you are new to lua, but += IS valid syntax for incrementing a variable, you can also do things like -=, *=, and /=.
  3. The :Clone() method is automatically inherited by all descendants of the Instance class, so it is 100% a valid method for our enemy Instance.
  4. wait() is actually an outdated and deprecated method(the most obvious reason you don’t know what you are talking about). There is a reason anyone that stays up to date on best practices uses task.wait(), as it is much more accurate.
    PLEASE PLEASE PLEASE don’t ever do this again, as you are wasting people’s time and just flooding a forum post with incorrect information.
1 Like

hmm why tho? basicly its bad. After a a specific time they not Walking animore they just run the the next checkpoint without reaching it. to tell it in that way

Humanoid:MoveTo() have an Timeout after 10 seconds.

I dont use that Methode so i dont have Timeouts.

Btw fixed it on my Self. And ill still Continue using CFrame instead of MoveTo bc MoveTo is realy bad.

function enemyFuncs:Move()
	local waypointIndex = 1
	local connection
	local hrp = self:FindFirstChild("HumanoidRootPart")
	local hum = self:FindFirstChild("Humanoid")
	
	connection = game:GetService("RunService").Heartbeat:Connect(function()
		if waypointIndex <= #waypoints:GetChildren() then
			if hum.Health == 0 then
				self:Destroy()
				connection:Disconnect()
			end
			local cwp = waypoints:GetChildren()[waypointIndex]
			local tP = Vector3.new(cwp.Position.X, hrp.Position.Y, cwp.Position.Z)
			local direction = (tP - hrp.Position).Unit
			local orientation = CFrame.new(hrp.Position, tP)
			hrp.CFrame = CFrame.new(direction * self.Speed.Value * (1/60)) * orientation  

			if (tP - hrp.Position).Magnitude < 0.1 then
				waypointIndex += 1
			end
		else
			self:Destroy()
			connection:Disconnect()
		end
	end)
end

thats how it looks like now.

btw thanks to Tell him that my Coding is Correct :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.