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
while true do
target = script.Parent.Target.Value
print(target)
pos = game.Workspace:FindFirstChild(target).Position
script.Parent.Humanoid:MoveTo(pos)
wait()
end
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.
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
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.
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?
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.