Orbiting part goes out of orbit and starts glitching out

Hello, It’s my first time posting on this dev forum!

So I have this menu I wanted to make so I started with adding 2 parts orbiting a “center” to make it look cooler.

However, it starts to glitch out for no reason after a set amount of time. I’m thinking it has something to do with the code

-- Variables
local RunService = game:GetService("RunService")

local NoteBookPrimaryPart = game.Workspace:WaitForChild("NotebookJ"):WaitForChild("PrimaryPart")

local  R6xer = game.Workspace:WaitForChild("R6"):WaitForChild("center")

local LockerPrimaryPart = game.Workspace:WaitForChild("Locker"):WaitForChild("Primary Part")

-- Variables for Orbiting

local Speed = 0.02
local Speed2 = 0.01
local i = 0 
local ind = 4
local distance = 8.5
local distance2 = 9
local Angle = math.rad(0)

RunService.Heartbeat:Connect(function()
	i = i + 1 
	ind = ind + 1
	NoteBookPrimaryPart.CFrame =R6xer.CFrame * CFrame.fromEulerAnglesXYZ(-i*Speed, Angle, i*Speed) * CFrame.new(0,0,distance)
	LockerPrimaryPart.CFrame = R6xer.CFrame * CFrame.fromEulerAnglesXYZ(ind*Speed2, math.rad(55),ind*Speed2) * CFrame.new(0,0,distance2)
	print(i)
end)

Here is what happens when I start the game: https://gyazo.com/4c014908621484c756b9364e0c25c37f

And for some reason after a few minutes it devolves into madness:
https://gyazo.com/acb518f22f34fd6c4bf97efcdbffc492

Any problems with the code or an alternative way to code it? I used a CFrame orbit tutorial from 9 years ago. Help would be appreciated :smiley:

i know you want a fix to your system you have right now, but is there no way that you can do this with tweens? you would have to rely on the math matching up perfectly

I’m not very good at roblox studio right now :frowning: I have no idea how do to it without scripting.

I honestly have no idea how to do it :sob:. I’ve found a few posts about the subject with some code you could try out though.

the last one has a model you can add to your game, but I would try to code it myself first.

Thanks g, really aprreciate it :V

no worries. let me know if any of them work for you!

1 Like

Hello whats the issue here?
Simply use this formula :laughing:
image
r: is the separation distance between the two bodies
theta: is the angle that r makes with the axis of periapsis

I could go on but that’s all you need to know.

1 Like

After a set amount of time the models glitch out. Haha, I managed to fix it through another forum. Thank you for the formula tho! :]