Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How could I fix my script so :MoveTo() works properly?

I have this script in ServerScriptService that should move a brick to a cannon. However, when I run the script, it says:

00:02:41.026 - Unable to cast Instance to Vector3 00:02:41.027 - Stack Begin 00:02:41.028 - Script 'ServerScriptService.Script.MoveEnemy', Line 1 00:02:41.028 - Stack End

Here is the code:

game.Workspace.Enemy.Humanoid:MoveTo(game.Workspace.Cannon.Cannon)

What could I do to make this work?

Thanks!

3 comments

0

game.workspace.cannon.cannon.Position/game.Workspace.Cannon.Position i think

0

I think you are meant to move with character e.g .Character:MoveTo(where)

0

and use game.Workspace.Cannon.Cannon.Position

2 answers

The function MoveTo requires the Vector3 that the humanoid will move to, it supports an optional argument which is an instance that is used to "follow" ie if its position updates so does the end point that the humanoid is walking to.

Example:-

local cannon = workspace.Cannon.Cannon
workspace.Enemy.Humanoid:MoveTo(cannon.Position) -- the Vector3 of the cannon

-- or you can use
workspace.Enemy.Humanoid:MoveTo(cannon.Position, cannon) -- this will update every time the cannon moves

I also have an example here

I hope this helps.

Vector3 is a coordinate in 3d space, in this case you want to MoveTo the position(Vector3) of the Cannon. You can do so by using the position property of the object(the cannon). According to your code, you would add ".Position" after ".Cannon" as seen in example A.

Example A:

game.Workspace.Cannon.Cannon.Position

I am not sure if you made an error here:

game.Workspace.Enemy.Humanoid:MoveTo(game.Workspace.Cannon.Cannon)

If you didn't feel free to skip over example B.

Example B:

game.Workspace.Cannon.Position

However, MoveTo is easier to use, I suggest using PathfindingService and here's why. This service has overall better results when it comes to moving a Character. Don't worry, I'll explain. Let's say there is a brick blocking the path of where the Character is moving to; the character would not reach the destination using MoveTo.

Try out this tutorial for pathfinding: http://wiki.roblox.com/index.php?title=Pathfinding

This discussion is preserved as it was. New answers, comments, and votes are closed.