Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do u calculate positions?

Asked by 2 years ago
Edited 2 years ago

I have to calculate a position in-between a tower and an enemy. This is the code

local tower = script.Parent local enemy = workspace.Enemy

local distance = tower.Position - enemy.Position

print(distance)

In the out put it says this

Position is not a valid member of Model "Workspace.Tower".

Stack Begin

Script 'Workspace.Tower.TowerTesting' , Line 4

Stack end

Is it because the tower I made comes from the Rig maker or am I just not coding right?

2 answers

Log in to vote
0
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
2 years ago

The error is letting you know that you're attempting to grab the Position of a Model rather than a BasePart. I don't have prior knowledge of the contents of your model, but in this situation it is ideal to set the PrimaryPart property of the model to be the tower's main (biggest?) part. Then to get the distance between the tower and an enemy, you could get the difference between the two position vectors and use its magnitude!

local tower = script.Parent
local enemy = workspace.Enemy
local distance = tower.PrimaryPart.Position - enemy.PrimaryPart.Position -- Here I am assuming enemy is a model. If it is not, then you can remove the PrimayPart part I used

print(distance.Magnitude.." studs") -- This is always in studs
0
Thank you, The distance calculated, and computerchris your answer also explaned how to fix it, but this one had a script to show, but you both saved A LOT of wasted time. epet6644 0 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

Model instances do not have a Position field. What you can do is use the PrimaryPart field of your Model.

Something like: tower.PrimaryPart.Position would suffice so long as you set the PrimaryPart via Script or using the Properties viewer in Studio.

Answer this question