# "X cannot be assigned to"

I wanted to randomly change the position of 2 parts called “StartPos and EndPos” by using:

``````local StartPos,EndPos = script.Parent,workspace.EndPos
while true do
StartPos.Position.X = EndPos.Position.X == math.random(-300,200)
StartPos.Position.Y = EndPos.Position.Y == math.random(-200,286)
wait(5)
end
``````

but I was met with “X cannot be assigned to”.
I would appreciate it if someone can tell me what did I do wrong, thank you!

I don’t understand what that means

``````StartPos.Position.X =  Vector3.new(EndPos.Position.X + math.random(-200, 286))
StartPos.Position.Y = Vector3.new(EndPos.Position.Y + math.random(-200, 286))
``````

maybe replace it with this

2 Likes

Sadly, you would have to assign it in this way:

``````local StartPos,EndPos = script.Parent,workspace.EndPos
while true do
StartPos.Position = Vector3.new(math.random(-300,200),math.random(-300,200),0)
end
``````

I dont think you could assign the X,Y and Z values separately like this.

3 Likes

EDIT: Actually no, forget about my post, because I never used the `EndPos` variable and I think that’s probably not what you meant, so it’s good if you explained what you wanted to make

`EndPos.Position.X == math.random(-300,200)` returns a bool (`true` or `false`), not a number, so your line

``````StartPos.Position.X = EndPos.Position.X == math.random(-300,200)
``````

is a logic error.

I think you meant for:

``````EndPos.Position.X = math.random(-300,200)
``````

here, because you don’t have to compare anything

Additionally `Position` is a changeable value, but `Position.X` or `Position.Y` is a readable value. A solution is to do this:

``````local StartPos,EndPos = script.Parent,workspace.EndPos
while true do
StartPos.Position = Vector3.new(math.random(-300,200), math.random(-200,286), 0)
wait(5)
end
``````
3 Likes

You can’t assign a boolean to it, when you declare “EndPos.Position.X == math.random(-300,200)” you really are asigned a boolean value. Apparently you need declare a position and then you match the positions. I don’t understand what you did. Can you explain me?

1 Like