Help with position script

Ahoy! What I would like to make is for an npc to move to the position of the string value that is placed in the NPC. However I end up getting this error:

09:33:21.923 Workspace.NPC.Script:4: attempt to index nil with ‘Position’ - Server - Script:4

This is the setup:

Here is the script:

while true do
	target = script.Parent.Target.Value
	print(target)
	pos = game.Workspace:FindFirstChild(target).Position
	script.Parent.Humanoid:MoveTo(pos)
	wait()
end

Help would be appreciated!

try with this:

because you can’t say “game.Workspace:FindFirstChild(target).Position

It still has this error

11:39:07.916 Workspace.NPC.Moveto:5: attempt to index nil with ‘Position’ - Server - Moveto:5

show me the target that is in workspace

in this screenshot I have the target which is a string value. The string’s value is what the target name should be. In my case I have a part named “E” that is in workspace so E is the value of the string.

no show me the “game.Workspace:FindFirstChild(“Target”)” not the target that is in the player the one that is in workspace

first of all is there a value that is printed when you print target if not then there is a problem assigning that value on the other hand with “MoveTo” i believe you use an intvalue not a stringvalue so you should probably change the instance “target” from a stringvalue to an intvalue

Check if target.Value is not equal to nil by using ~=

you can just use the :MoveTo() in the model and not the humanoid or anything else, ive done that before though not reccomended

Alrighty so here’s a few problems with your code. What I’m assuming you’re trying to do is add a targets name to the string value, then trying to find it in the workspace? If so, then you can try this:

If you’re target is a Model object then it obviously won’t work because Models themselves don’t actually store positions, the parts inside them do however. So if it was a rig you could use tar = game.Workspace:FindFirstChild(target):WaitForChild(“HumanoidRootPart”)
Then you could use MoveTo(tar.Position)

while true do
target = script.Parent.Target.Value
print(target)
tar = game.Workspace:FindFirstChild(target)
TarPos = tar.Position
script.Parent.Humanoid:MoveTo(TarPos)
wait()
end

I hope that helped! If you have any issues, reply to moi and we’ll figure it out. If this did solve your issue then ya know what to do!

Target is a string value
Position is a vector 3 value.
You can’t do Target.Position as string values don’t have position properties, they just hold a value.

First, you need to either get three separate string values to hold the three X,Y,Z coordinates, or have them placed in one place with , or something to separate them.

Then, you have to convert each to a number value with tonumber

After this, just set the position of X, Y, Z separately.

ie
Position.X =
Position.Y =
Position.Z =

An int value is a number value I can change the instance “target” to a number.

yes target should definitely be a number because if your setting it to move to target position “moveto” only works with numbers, what sort of values have you been setting target to?

I had to do a few more things to the script and it worked, thank you!

You MUST change the integer value to a number.
Positions have decimal points after them,
However you can use things like math.floor() to round down the number value to an integer, and still use int values to store it.

I made the script work with keeping it as a string value. But thanks for the help!

1 Like