Issues with MoveTo()

So I am working on a simple project to be able to shot targets, but I am getting 2 issues. The NPCs are not animating when they are being moved, and will sometimes just stop.

Video

I use different collision groups for the Rigs and player joined. The whole of the NPCs moving is ran on a ServerScript. Here is the code I am using:

local rs = game:GetService("ReplicatedStorage")
local PreFabs = rs.PreFabs
local Events = rs.Events

local chars = PreFabs.Chars
local boy = chars.Boy
local girl = chars.Girl
local rthro = chars.Rthro

local StartGame = Events.StartGame
local EnemyKilled = Events.EnemyKilled
local PassInfo = Events.PassInfoClient
local WaveCompleted = Events.WaveCompleted

local _workspace = game:GetService("Workspace")
local projHold = _workspace:FindFirstChild("ProjHold")
local spawnPoints = _workspace:FindFirstChild("RandomSpawns")
local endPoints = _workspace:FindFirstChild("EndResult")

local waveCount = 1
local Enimies = 0
local totalEnemy = nil

local wavesMod = require(script.Rounds)
local GameUI = game:GetService("StarterGui").Game
local WaveTitle = GameUI.WaveTitle
local EnimiesLeft = GameUI.EnemiesLeft

local completed = 0

local function setUpChars(gender, spawnPos, walkSpeed, endPoint, timeToEnd, player)
	task.spawn(function()
		local new = gender:Clone()
		new.Parent = workspace
		new:PivotTo(spawnPos.CFrame)
		new.Humanoid.WalkSpeed = walkSpeed

		new.Humanoid:MoveTo(endPoint.Position)
		task.wait(timeToEnd)
		completed += 1
		if new.Humanoid.Health ~= 0 then
			print("Lost")
			EnemyKilled:FireAllClients()
			player.Character.Humanoid.Health -= 10
			new:Destroy()
		else
			new:Destroy()
		end
	end)
end


StartGame.OnServerEvent:Connect(function(player)
	local wave = wavesMod["Round"..waveCount]
	local amountOfChars = wave["AmountOfChars"]
	Enimies = amountOfChars
	local delayWave = wave["DelayUntilNextWave"]
	player.GameSettings.Ammo.Value = wave["Ammo"]

	PassInfo:FireAllClients(amountOfChars, waveCount)

	for i = 1, amountOfChars do
		local gender = math.random(1, 3)
		local spawns = math.random(1, 8)
		local spawnPos = spawnPoints:FindFirstChild(spawns)
		local endPoint = endPoints:FindFirstChild(spawns)
		if spawnPos:GetAttribute("Taken") == true then
			repeat task.wait()
				spawns = math.random(1, 8)
				spawnPos = spawnPoints:FindFirstChild(spawns)
				endPoint = endPoints:FindFirstChild(spawns)

			until spawnPos:GetAttribute("Taken") == false
		end
		spawnPos:SetAttribute("Taken", true)
		local Speeds = wave["PossibleSpeed"]

		local walkSpeed = math.random(Speeds["MinSpeed"], Speeds["MaxSpeed"])
		local timeToEnd = 54/walkSpeed

		if gender == 1 then
			setUpChars(boy, spawnPos, walkSpeed, endPoint, timeToEnd, player)
		elseif gender == 2 then
			setUpChars(girl, spawnPos, walkSpeed, endPoint, timeToEnd, player)
		elseif gender == 3 then
			setUpChars(rthro, spawnPos, walkSpeed, endPoint, timeToEnd, player)
		end

		task.wait(wave["DelayTime"])
	end

	repeat task.wait(0.25) print(completed) until completed == amountOfChars
	WaveCompleted:FireAllClients(delayWave)
	task.wait(delayWave)
	completed = 0

	waveCount += 1
	for _, x in ipairs(spawnPoints:GetChildren()) do
		x:SetAttribute("Taken", false)
	end

	StartGame:FireClient(player)
end)

game:GetService("Players").PlayerAdded:Connect(function(plr)
	plr.CharacterRemoving:Connect(function()
		plr:Kick("PLAYER HAS DIED")
	end)
end)

If you also want to see the module code here:

local module = {
	["Round1"] = {
		["AmountOfChars"] = 3,
		["DelayTime"] = 0.3,
		["PossibleSpeed"] = {
			["MaxSpeed"] = 10,
			["MinSpeed"] = 3
		},
		["DelayUntilNextWave"] = 5,
		["Ammo"] = 10
	},
	["Round2"] = {
		["AmountOfChars"] = 6,
		["DelayTime"] = 3,
		["PossibleSpeed"] = {
			["MaxSpeed"] = 20,
			["MinSpeed"] = 3
		},
		["DelayUntilNextWave"] = 5,
		["Ammo"] = 12
	},
	["Round3"] = {
		["AmountOfChars"] = 5,
		["DelayTime"] = 1,
		["PossibleSpeed"] = {
			["MaxSpeed"] = 20,
			["MinSpeed"] = 3
		},
		["DelayUntilNextWave"] = 5,
		["Ammo"] = 10
	},
	["Round4"] = {
		["AmountOfChars"] = 7,
		["DelayTime"] = 2,
		["PossibleSpeed"] = {
			["MaxSpeed"] = 25,
			["MinSpeed"] = 5
		},
		["DelayUntilNextWave"] = 5,
		["Ammo"] = 16
	},
	["Round5"] = {
		["AmountOfChars"] = 4,
		["DelayTime"] = 0.5,
		["PossibleSpeed"] = {
			["MaxSpeed"] = 15,
			["MinSpeed"] = 3
		},
		["DelayUntilNextWave"] = 5,
		["Ammo"] = 6
	},
	["Round6"] = {
		["AmountOfChars"] = 8,
		["DelayTime"] = 2,
		["PossibleSpeed"] = {
			["MaxSpeed"] = 25,
			["MinSpeed"] = 5
		},
		["DelayUntilNextWave"] = 5,
		["Ammo"] = 22
	},
	["Round7"] = {
		["AmountOfChars"] = 5,
		["DelayTime"] = 1.5,
		["PossibleSpeed"] = {
			["MaxSpeed"] = 20,
			["MinSpeed"] = 5
		},
		["DelayUntilNextWave"] = 5,
		["Ammo"] = 10
	},
	["Round8"] = {
		["AmountOfChars"] = 6,
		["DelayTime"] = 2.5,
		["PossibleSpeed"] = {
			["MaxSpeed"] = 25,
			["MinSpeed"] = 5
		},
		["DelayUntilNextWave"] = 5,
		["Ammo"] = 15
	},
	["Round9"] = {
		["AmountOfChars"] = 5,
		["DelayTime"] = 2,
		["PossibleSpeed"] = {
			["MaxSpeed"] = 20,
			["MinSpeed"] = 5
		},
		["DelayUntilNextWave"] = 5,
		["Ammo"] = 12
	},
	["Round10"] = {
		["AmountOfChars"] = 7,
		["DelayTime"] = 3,
		["PossibleSpeed"] = {
			["MaxSpeed"] = 30,
			["MinSpeed"] = 5
		},
		["DelayUntilNextWave"] = 5,
		["Ammo"] = 10
	},
	["Round11"] = {
		["AmountOfChars"] = 4,
		["DelayTime"] = 1,
		["PossibleSpeed"] = {
			["MaxSpeed"] = 15,
			["MinSpeed"] = 3
		},
		["DelayUntilNextWave"] = 5,
		["Ammo"] = 5
	},
	["Round12"] = {
		["AmountOfChars"] = 8,
		["DelayTime"] = 2,
		["PossibleSpeed"] = {
			["MaxSpeed"] = 30,
			["MinSpeed"] = 5
		},
		["DelayUntilNextWave"] = 5,
		["Ammo"] = 12
	},
	["Round13"] = {
		["AmountOfChars"] = 5,
		["DelayTime"] = 1.5,
		["PossibleSpeed"] = {
			["MaxSpeed"] = 25,
			["MinSpeed"] = 5
		},
		["DelayUntilNextWave"] = 5,
		["Ammo"] = 5
	},
	["Round14"] = {
		["AmountOfChars"] = 7,
		["DelayTime"] = 2.5,
		["PossibleSpeed"] = {
			["MaxSpeed"] = 30,
			["MinSpeed"] = 5
		},
		["DelayUntilNextWave"] = 5,
		["Ammo"] = 8
	},
	["Round15"] = {
		["AmountOfChars"] = 6,
		["DelayTime"] = 3,
		["PossibleSpeed"] = {
			["MaxSpeed"] = 25,
			["MinSpeed"] = 5
		},
		["DelayUntilNextWave"] = 5,
		["Ammo"] = 6
	}
}

return module

Please help me figure this out! Thanks!

Hey @Stephenesta_aqui! Are you receiving any warnings or errors in your output? This will help with debugging and help focus on a specific part in the script where the issue could be happening.

The thing is there are no issues, and when I do print statements, nothing seems overtly off. Every part of the script is reaching where it needs to, but it just stops.

Turns out the ‘MoveTo()’ function has a timeout of around 8 seconds, so anything that takes longer than 8 seconds would require you to create a repeat function.

Here is a sample of code that I found, that does also work for my game.

Link to the DevForum: Humanoid:MoveTo() with no timeout - #4 by KinqAndi

Here is how I used that into my code:

function MoveNPC(Point, Character)
	local HRP = Character.HumanoidRootPart
	repeat
		wait()
		Character.Humanoid:MoveTo(Point)
	until (HRP.Position - Vector3.new(Point.X, HRP.Position.Y, Point.Z) ).magnitude <= 1
end

local function setUpChars(gender, spawnPos, walkSpeed, endPoint, timeToEnd, player)
	local thread = task.spawn(function()
		local new = gender:Clone()
		new.Parent = workspace
		new:PivotTo(spawnPos.CFrame)
		new.Humanoid.WalkSpeed = walkSpeed

		MoveNPC(endPoint.Position, new)
		completed += 1
		if new.Humanoid.Health ~= 0 then
			EnemyKilled:FireAllClients()
			player.Character.Humanoid.Health -= 10
			new:Destroy()
		else
			new:Destroy()
		end
	end)
end

For the animating part, I saw the LocalScript inside of the Rig and copy and pasted all the children and the code into a Script and childed it to the Rig, and now the animations work.

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