How would you detect a marble's place from a marble race? (e.g. 1st, 2nd, 3rd or 1, 2, 3, ETC)

I’m currently making a marble race and I want it so the marble that’s currently in the lead in everyone or the marble that has the lowest X position, is 1st and then the marble after that is 2nd, and ETC. Here’s my script of the marble logic, but it doesn’t include this feature as of currently.

Script:

--!optimize 2

local faces = true
local randomSizes = true
local randomMaterials = true
local randomTextures = false
local time = 0.00

local function limitTo1Dp(num: number): string
	return string.format("%.2f", num)
end


task.spawn(function()
	game:GetService("RunService").Heartbeat:Connect(function(delta)

		time += delta
	end)
end)

local Materials = {
	Enum.Material.Air,
	Enum.Material.Asphalt,
	Enum.Material.Basalt,
	Enum.Material.Brick,
	Enum.Material.Cobblestone,
	Enum.Material.Concrete,
	Enum.Material.CorrodedMetal,
	Enum.Material.CrackedLava,
	Enum.Material.DiamondPlate,
	Enum.Material.Fabric,
	Enum.Material.Foil,
	Enum.Material.Glacier,
	Enum.Material.Granite,
	Enum.Material.Grass,
	Enum.Material.Ground,
	Enum.Material.Ice,
	Enum.Material.LeafyGrass,
	Enum.Material.Limestone,
	Enum.Material.Marble,
	Enum.Material.Metal,
	Enum.Material.Mud,
	Enum.Material.Neon,
	Enum.Material.Pavement,
	Enum.Material.Pebble,
	Enum.Material.Plastic,
	Enum.Material.Rock,
	Enum.Material.Salt,
	Enum.Material.Sand,
	Enum.Material.Sandstone,
	Enum.Material.Slate,
	Enum.Material.SmoothPlastic,
	Enum.Material.Snow,
	Enum.Material.Wood,
	Enum.Material.WoodPlanks
}


local textureList = {
	Enum.SurfaceType.Studs,
	Enum.SurfaceType.Weld,
	Enum.SurfaceType.Smooth,
	Enum.SurfaceType.Glue,
	Enum.SurfaceType.Universal,
	Enum.SurfaceType.Inlet
}

local Names = {
	"Aiden","Blaze","Carter","Dexter","Eli","Finn","Grayson","Hudson","Isaac","Jaxon",
	"Kai","Leo","Mason","Nolan","Owen","Parker","Quinn","Ryder","Sawyer","Tyler",
	"Uriah","Victor","Wyatt","Xander","Yusuf","Zane","Arlo","Brody","Caleb","Dylan",
	"Emmett","Felix","Gavin","Henry","Ivan","Jude","Knox","Landon","Micah","Noah",
	"Orion","Preston","Reed","Silas","Tristan","Ulysses","Vaughn","Wesley","Zachary","Asher",

	"Bennett","Colton","Dominic","Ezekiel","Franklin","George","Harrison","James","Kyle","Logan",
	"Malachi","Nathan","Oscar","Phoenix","Roman","Sebastian","Theo","Vincent","William","Xavier",
	"Yosef","Zion","Axel","Bryson","Corbin","Declan","Easton","Finnegan","Garrett","Holden",
	"Ian","Jonah","Kieran","Luca","Miles","Nico","Oliver","Pierce","Quincy","Rowan",
	"Spencer","Tobias","Ulric","Weston","Zeke","Aaron","Brandon","Chase","Daniel","Evan",

	"Flynn","Gianni","Hector","Ismael","Jasper","Kaden","Leon","Marco","Neil","Otis",
	"Paxton","Rafael","Santino","Tanner","Uriel","Valen","Walker","Zander","Alfred","Beckett",
	"Cedric","Damian","Ellis","Forest","Gideon","Harvey","Ira","Julian","Keaton","Lawson",
	"Matteo","Nash","Onyx","Porter","Remy","Sterling","Titan","Vito","Warren","Zayden",

	"Atlas","Bodhi","Cyrus","Dante","Elijah","Fletcher","Greyson","Hugo","Indigo","Jett",
	"Kellan","Lyle","Maddox","Nehemiah","Oakley","Pablo","Ronan","Soren","Talon","Vance", "Ryan", "O'Neil"
}

for i,v in pairs(workspace:FindFirstChild("balls"):GetChildren()) do
	if v and v:IsA("BasePart") then
		v.Massless = true
		v:SetNetworkOwnershipAuto()
		local billboard = Instance.new("BillboardGui", v)
		billboard.SizeOffset = Vector2.new(0,1.25)
		billboard.Size = UDim2.new(8,0,8,0)
		local imagelabel = Instance.new("TextLabel", billboard)
		imagelabel.Size = UDim2.new(1,0,1,0)
		imagelabel.TextScaled = true
		imagelabel.TextColor3 = Color3.fromRGB(255,255,255)
		imagelabel.BackgroundTransparency = 1
		local randomyes = math.random(1, #Names)
		local yesyes = Names[randomyes]
		imagelabel.Text = yesyes.." #"..i.." Time: "..limitTo1Dp(time)
		task.spawn(function()
			game:GetService("RunService").Heartbeat:Connect(function(delta)
				imagelabel.Text = yesyes.." #"..i.." Time: "..limitTo1Dp(time)
			end)
		end)
		task.spawn(function()
			while true do
				task.wait(math.random(20,120))
				task.spawn(function()
					if faces then
						if v:FindFirstChildOfClass("Decal") then
							v:FindFirstChildOfClass("Decal").Texture = "rbxassetid://15603094408"
							task.wait(1)
							if v:FindFirstChildOfClass("Decal") then

								v:FindFirstChildOfClass("Decal").Texture = "rbxassetid://925874207"
							end
						end
					end
				end)
				local ex = Instance.new("Explosion", v)
				ex.DestroyJointRadiusPercent = 0
				ex.Position = v.Position
				ex.BlastRadius = 3000000
				ex.BlastPressure = 1000000

			end
		end)
		if randomMaterials then
			if randomTextures then
				warn("Random Materials and Random Textures were both enabled! Please only enable one of them next time.")
				randomTextures = false
				v.Material = Materials[math.random(1,#Materials)]
			end
			v.Material = Materials[math.random(1,#Materials)]
		end
		if randomSizes then
			local randomSizeYes = math.random(5,14)
			v.Size = Vector3.new(randomSizeYes, randomSizeYes, randomSizeYes)
		end
		if randomTextures then
			local randomTexturesYes = math.random(1, #textureList)

			v.TopSurface = textureList[randomTexturesYes]
			v.BottomSurface = textureList[randomTexturesYes]
			v.LeftSurface = textureList[randomTexturesYes]
			v.RightSurface = textureList[randomTexturesYes]
			v.FrontSurface = textureList[randomTexturesYes]
			v.BackSurface = textureList[randomTexturesYes]

		end
		if faces then
			local d = Instance.new("Decal", v)
			d.Texture = "rbxassetid://925874207"
		end

		v.Name = yesyes.." | #"..i

		v.Color = Color3.fromRGB(math.random(0,255), math.random(0,255), math.random(0,255))
	end
end

Is it exactly the lowest X position, or is it possible the track could be changed to have turns or a unique shape?

For the first case, you just sort a table based on position.

local players = { --[[List of marbles as BaseParts]] }

-- Sort marbles by distance to a "GOAL". players[1] will point to the last marble, players[#players] will be first.
table.sort(players, function(a: BasePart, b: BasePart) return math.abs(GOAL - a.Position.X) < math.abs(GOAL - b.Position.X) end)

For the second case, it’s similar, but most people use checkpoints and calculate based on distance to checkpoints. If a player is in a further forward checkpoint, they’re automatically higher on the list, but if they’re on the same checkpoint, then you do the comparison by distance to next checkpoint.

I’d like doing the lowest X position exactly.