Help with moving npc

Hey, so I want to make a remote event where I can just move an NPC, this is the script

local Helza = game.Workspace.Helza
local WalkAnim = Instance.new("Animation")
WalkAnim.AnimationId = "http://www.roblox.com/asset/?id=5319844329"
local HelzaMoveEvent = game.ReplicatedStorage.Remotes.HelzaMoveEvent

local controller = Helza.Humanoid

HelzaMoveEvent.OnClientEvent:Connect(function(EndPosition)
	while Helza.HumanoidRootPart.Position ~= EndPosition do
		controller:LoadAnimation(WalkAnim):Play()
		Helza.Humanoid:MoveTo(EndPosition)
	end
		controller:LoadAnimation(WalkAnim):Stop()
end)

end)

But when I do that, my roblox studio just crash and become not responding

Can anyone help me? Whats wrong with my code?

1 Like

does the MoveTo() function need to be in the while loop same with playing the animation just make the animation loop and only play it before the while loop

1 Like

This should work :))
(it should have been this part that broke it: while Helza.HumanoidRootPart.Position ~= EndPosition do)

local Helza = game.Workspace.Helza
local WalkAnim = Instance.new("Animation")
WalkAnim.AnimationId = "http://www.roblox.com/asset/?id=5319844329"
local HelzaMoveEvent = game.ReplicatedStorage.Remotes.HelzaMoveEvent

local controller = Helza.Humanoid

HelzaMoveEvent.OnClientEvent:Connect(function(EndPosition)
	controller:LoadAnimation(WalkAnim):Play()
	Helza.Humanoid:MoveTo(EndPosition)
	Helza.Humanoid.MoveToFinished:wait()
	controller:LoadAnimation(WalkAnim):Stop()
end)

(idk if it’ll work, i can’t test it atm)

2 Likes

I want to play it so that, if the npc get into the location I want, they stop the animation. So I put the MoveTo() function inside the while loop

Thank you so much, but when the npc touch/got on the destination, the animation doesnt stop

1 Like
local Helza = game.Workspace.Helza
local WalkAnim = Instance.new("Animation")
WalkAnim.AnimationId = "http://www.roblox.com/asset/?id=5319844329"
local HelzaMoveEvent = game.ReplicatedStorage.Remotes.HelzaMoveEvent

local controller = Helza.Humanoid
local Animation = controller:LoadAnimation(WalkAnim)

HelzaMoveEvent.OnClientEvent:Connect(function(EndPosition)
    Animation:Play()
    controller:MoveTo(EndPosition)
    controller.MoveToFinished:Wait()
    Animation:Stop()
end)

It crashes because you did not add a wait in the while loop but a loop is unnecessary in this situation.

The script above is assuming that your distance is no longer than 8 second, the timeout distance.

2 Likes
local Helza = game.Workspace.Helza
local WalkAnim = Instance.new("Animation")
WalkAnim.AnimationId = "http://www.roblox.com/asset/?id=5319844329"
local HelzaMoveEvent = game.ReplicatedStorage.Remotes.HelzaMoveEvent



local controller = Helza.Humanoid

local animation = controller:LoadAnimation(WalkAnim)

HelzaMoveEvent.OnClientEvent:Connect(function(EndPosition)
	animation:Play()
	Helza.Humanoid:MoveTo(EndPosition)
	Helza.Humanoid.MoveToFinished:wait()
	animation:Stop()
end)
1 Like

Thank you so much, this is working perfectly!

1 Like

No worries, glad to help :))

Have fun!

1 Like

Thank you so much for your help but the animation doesnt stop when its reaching the destination

Why are you using the RBXScriptSignal’s deprecated wait method “:wait()”? “:Wait()” should be used instead.

The two provided scripts are essentially the same.

1 Like

What do you mean? I dont understand

Why does it matter? they both do the same thing.

1 Like

Ikr, thats why im confused, both thing do the same

1 Like

They behave similarly, their core definitions are different and :wait() is deprecated, :Wait() is the up to date variant and should be used instead.

The same applies for any deprecated instance method and its contemporary variant, consider :clone() and :Clone() for example, or :remove() and :Destroy().

1 Like

Oh got it, thank you so much for the knowledge!