Help with CFrame

I’m making a system that will generate the slots, but I’m having trouble positioning them correctly depending on the player’s island orientation.

The parts are being positioned like this:

But it should look like this:

Code here:

function SlotService.LoadSlots(Data : TypeData.PlayerData, SlotModel : Model, TopGrass : Part, Player : Player)
	local Row = {} :: {[number] : {Model}}
	local CurrentRow : {Model} = {}
	local PastSlot : Model?

	local TerrainSize = TopGrass.Size
	local TerrainCFrame = TopGrass.CFrame
	local BorderDistance = 10
	local StartPosition = TerrainCFrame * CFrame.new((-TerrainSize.X/2 + BorderDistance), TerrainSize.Y ,TerrainSize.Z/2 + BorderDistance)
	
	local UserFolder = Workspace:FindFirstChild('UserFolder_' .. Player.UserId)
	while not UserFolder do task.wait() end
	
	local TotalSlots = Data.Slots
	local RowCount = 1	
	Row[RowCount] = {}
	CurrentRow = Row[RowCount]
	
	local MAX_PER_ROW = 7
	local SLOT_OFFSET = 1.15
	for i = 1, TotalSlots do
		local isFirst = (i % MAX_PER_ROW == 1)
		if #CurrentRow >= MAX_PER_ROW then
			RowCount += 1
			Row[RowCount] = {}
			CurrentRow = Row[RowCount]

			if PastSlot then
				PastSlot = nil
			end	
		end
		
		local newSlot = SlotModel:Clone()
		newSlot.Parent = UserFolder
		newSlot.Name = 'Slot_' .. i

		if PastSlot ~= nil then
			local Slot = PastSlot :: Model
			local SlotCFrame = Slot:GetPivot() 
			newSlot:PivotTo(CFrame.new(SlotCFrame.Position + SlotCFrame.RightVector  * BorderDistance))
		else
			newSlot:PivotTo(CFrame.new(StartPosition.Position + StartPosition.LookVector * 15 + Vector3.new(0, 0, i * SLOT_OFFSET)))
		end

		table.insert(CurrentRow, newSlot)
		PastSlot = newSlot	
	end
end

@af_2048 can you help me please??

Try setting the CFrame relative to the slot cframe rather than reconstructing it, since your current method does not consider the orientation of the slot.

function SlotService.LoadSlots(Data : TypeData.PlayerData, SlotModel : Model, TopGrass : Part, Player : Player)
	local Row = {} :: {[number] : {Model}}
	local CurrentRow : {Model} = {}
	local PastSlot : Model?

	local TerrainSize = TopGrass.Size
	local TerrainCFrame = TopGrass.CFrame
	local BorderDistance = 10
	local StartPosition = TerrainCFrame * CFrame.new((-TerrainSize.X/2 + BorderDistance), TerrainSize.Y ,TerrainSize.Z/2 + BorderDistance)
	
	local UserFolder = Workspace:FindFirstChild('UserFolder_' .. Player.UserId)
	while not UserFolder do task.wait() end
	
	local TotalSlots = Data.Slots
	local RowCount = 1	
	Row[RowCount] = {}
	CurrentRow = Row[RowCount]
	
	local MAX_PER_ROW = 7
	local SLOT_OFFSET = 1.15
	for i = 1, TotalSlots do
		local isFirst = (i % MAX_PER_ROW == 1)
		if #CurrentRow >= MAX_PER_ROW then
			RowCount += 1
			Row[RowCount] = {}
			CurrentRow = Row[RowCount]

			if PastSlot then
				PastSlot = nil
			end	
		end
		
		local newSlot = SlotModel:Clone()
		newSlot.Parent = UserFolder
		newSlot.Name = 'Slot_' .. i

		if PastSlot ~= nil then
			local Slot = PastSlot :: Model
			local SlotCFrame = Slot:GetPivot() 
			newSlot:PivotTo(SlotCFrame.CFrame * CFrame.new(BorderDistance, 0, 0))
		else
			newSlot:PivotTo(StartPosition.CFrame * CFrame.new(i * SLOT_OFFSET, 0, -15))
		end

		table.insert(CurrentRow, newSlot)
		PastSlot = newSlot	
	end
end

Thanks so much! I just made a few adjustments, but it worked fine. CFrame is really tough for me to deal with

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