[URGENT] How to make something orbit the biggest object in the workspace?

Hello. I am currently working on a “Solar system” sandbox, however, I don’t know how to calculate an orbital period around the most massive object. Let’s say I added a sun, and then I added a planet, how would I make the planet orbit the “sun”?

1 Like

Use line forces so you don’t need to lose a single brain cell doing maths

local RunService = game:GetService("RunService")

local Planet = script.Parent
local TargetPlanet = nil

local PlanetDirection = Vector3.new(1.5, 0, 0) -- change X until it works
local Pull = 1000

local LineForce = Instance.new("LineForce")
LineForce.Visible = false
LineForce.ApplyAtCenterOfMass = true
LineForce.InverseSquareLaw = true
LineForce.ReactionForceEnabled = true
LineForce.Magnitude = Pull
LineForce.Parent = workspace

local Attachment1 = Instance.new("Attachment")
Attachment1.Parent = Planet

local Attachment2 = Instance.new("Attachment")
Attachment2.Parent = TargetPlanet

LineForce.Attachment0 = Attachment1
LineForce.Attachment1 = Attachment2

workspace.Gravity = 0
Planet.Velocity = PlanetDirection
Planet.Anchored = false

local function GetBiggestPart()
	local HighestMagnitude = 1
	local SelectedPart = nil

	for i, Part in pairs(workspace:GetDescendants()) do
		if Part:IsA("BasePart") and Part ~= workspace.Terrain and Part.Size.Magnitude > HighestMagnitude and Part ~= Planet then
			HighestMagnitude = Part.Size.Magnitude
			SelectedPart = Part
		end
	end

	return SelectedPart
end

RunService.Heartbeat:Connect(function()
	TargetPlanet = GetBiggestPart()
	
	if Attachment2.Parent ~= TargetPlanet then
		Attachment2.Parent = TargetPlanet
	end
end)
1 Like

Sorry for late reply but where do i put the script

1 Like

the something

1 Like

:sweat_smile: ah ok thanks

filling out requirements

Oh I forgot to say that… its kinda like a tycoon and it just orbits the same size soo… .

1 Like

Make your own gravity system and create a perspective on the client side, so you can get real universe sizes.

1 Like