Is there a reason why we cannot assign most properties of Roblox datatypes programmatically?

im a rather curious person so i make forums like this

An example would be Vector3

For some reason we cannot do something like this

local v3 = Vector3.new(4, 2, 67);
v3.X = 24;

There is often times you need to only change a specific axis.

You get an error “X cannot be assigned to”.

Why is this?

I highly doubt it is security since what would be abused? Nowhere is it cleared up in the wiki and I’ve looked for it in other posts.

1 Like

You can’t change it that way because it’s not a changable value. What I mean by this is that it’s a return value like it’s not meant to be changed but to display a value.

if you want to change a specific value like that you have to make it equal to its own class with the changed value you want.

For example:

local var = Vector3.new(1,2,3)
--I want to change it's Y to 5
var = Vector3.new(1,5,3)
--If I don't want to keep tying in the same values for the other values over and over
var = Vector3.new(var.X,5,var.Z)
--We can make this to a function

function changeVectorSettings(
	var,
	x,
	y,
	z
	)
	------ You leave the values you don't want to change nil --------
	if not x then
		x = var.X
	end
	if not y then
		y = var.Y
	end
	if not z then
		z = var.Z
	end
	--------------------
	var = Vector3.new(x,y,z)
end
--[[
	The values that are nill are going 
	to be the same as they were before 
	and will not be affected, of cource 
	you don't really need a function like 
	this most of the time tho :p If you do
	it would be best to put this in a 
	module script
--]]
changeVectorSettings(var,nil,5,nil) 

It would require more functions to be present on the C++ side for accessing/setting. Probably the reason.

Other than that it’s a lot easier to just create a new struct/class on the C++ side with the initializer values already in place (with the constructor) than it is to edit an already present one. This also presents a great field of optimizations if you know your data type’s fields won’t change after initialization.

8 Likes