Part Distribution not working properly

I have this vertical treadmill type minigame model, and it rotates and moves fine, except the parts don’t get evenly distributed.
Like some parts will be very close together, while other parts might be slightly close together, and some not close together at all

Code block:

function QuestsManager:StartQuest1(questIndex: number)
	print("Starting Quest 1 logic!")

	local basePart = workspace["QUEST 1"].Treadmills.Treadmill45.Base
	local platforms = workspace["QUEST 1"].Treadmills.Treadmill45.TweenParts:GetChildren()

	local moveRate = 5 -- studs in second
	local width = 7
	local height = 17
	local centerOffset = Vector3.new(-3.5, 0, 0) -- the offset from the center of the treadmill base
	local center = basePart.Position + centerOffset
	local totalPathLength = 2 * (width + height)
	local spacing = totalPathLength / #platforms

	local platformData = {}
	for index, platform in ipairs(platforms) do
		table.insert(platformData, {
			Part = platform,
			Progress = (index - 1) * spacing,
		})
	end

	RunService.Heartbeat:Connect(function(dt)
		for _, platform in ipairs(platformData) do
			platform.Progress += moveRate * dt
			if platform.Progress > totalPathLength then
				platform.Progress -= totalPathLength
			end

			local pos
			if platform.Progress <= width then
				pos = center + Vector3.new(platform.Progress, height / 2, 0)
			elseif platform.Progress <= width + height then
				pos = center + Vector3.new(width, height / 2 - (platform.Progress - width), 0)
			elseif platform.Progress <= 2 * width + height then
				pos = center + Vector3.new(width - (platform.Progress - width - height), -height / 2, 0)
			else
				pos = center + Vector3.new(0, -height / 2 + (platform.Progress - 2 * width - height), 0)
			end

			platform.Part.CFrame = CFrame.new(pos)
		end
	end)
end

Any help would be greatly appreciated.

Hello, this should help you.

Try these changes:

This will adjust the spacing calculation

  • Original Code: local spacing = totalPathLength / #platforms
  • Revised Code: local spacing = totalPathLength / (#platforms - 1)

This should make it start at the correct position

  • Original Code: Progress = (index - 1) * spacing
  • Revised Code: Progress = index * spacing

Recap:

  • Evenly distributed spacing calculation: local spacing = totalPathLength / (#platforms - 1)
  • Adjusted platform initialization for progress: Progress = index * spacing

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