Unable to assign value to CFrame.X

I’m trying to make a script that places a part on a floor at a random position on it, im setting the position to the middle and then adding the value of the floors position, I’m having trouble setting the random position

Error :
image

Script :

local X = script.Parent.Size.X
local Y = script.Parent.Size.Y
local Z = script.Parent.Size.Z + 0.1
local Center = Floor:GetBoundingBox()


local function messSpawn()
	
	local XPos = Vector3.new(math.random(0, X))
	local YPos = Vector3.new(math.random(0, Y))

	
	--spawn thing
	local Mess = game.ReplicatedStorage.Mess:Clone()
	
	Mess.Parent = script.Parent.Messes
	Mess.Anchored = true
	Mess.CFrame = Center
	
	Mess.CFrame.X = CFrame.new(XPos)
	Mess.CFrame.X = CFrame.new(YPos)

	
	print("Y = "..Y, "X = "..X)
	
end

local Event = game.ReplicatedStorage.RemoteEvent

Event.OnServerEvent:Connect(messSpawn)

Does this work?

Mess.CFrame = CFrame.new(XPos, YPos, Mess.CFrame.Position.Z)

Nope, now they all spawn in the same spot, quite weird

Using @drewbluewasabi’s code, replace XPos with this:

local XPos = math.random(0, X)

and likewise for YPos

In addition to this, you need to now assign the cframes like so:

Mess.CFrame = CFrame.new(Vector3.new(XPos, YPos, 0))

Or just set the position directly:

Mess.Position = Vector3.new(XPos, YPos, 0)

im asnwering about the error.
CFrame is a readonly object. you cannot set any properties to a CFrame object, but you can change an instance’s CFrame.

2 Likes

I was able to figure it out, heres the working script if anyone needs it

local X = tostring(script.Parent.Size.X / 2) 
local Y = tostring(script.Parent.Size.Y + 0.1)  --up n down
local Z = tostring(script.Parent.Size.Z / 2)
local Center = Floor:GetBoundingBox()
local CenterStr = tostring(Center)
local MessFolder = script.Parent

MaxInstanceSetting = 10  --roughly the instances allowed at any one time

MaxInstances = false
--setting the center of the area

local MiddlePart = Instance.new("Part")

MiddlePart.Anchored = true
MiddlePart.Parent = script.Parent
MiddlePart.Name = "probably the middle of the floor"
MiddlePart.CFrame = Center
MiddlePart.Size = Vector3.new(1, 2, 1)




local function messSpawn()

	local XPos = tonumber(math.random(-X, X)) 
	local ZPos = tonumber(math.random(-Z, Z)) 
	local V3Pos = Vector3.new(XPos, Y, ZPos)
	local FinalPos = Vector3.new(XPos, Y, ZPos)

	--spawn thing
	local Mess = game.ReplicatedStorage.Mess:Clone()
	Mess.CFrame = MiddlePart.CFrame + FinalPos
	Mess.Parent = script.Parent.Messes
	Mess.Anchored = true
	

end```

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.