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

How do u calculate positions?

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

✓ Accepted answer

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

1 comment

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.

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.

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