How can I add on to this CFrame script?

Down below I have a working script:


Now I have a question about so script…
– If I want to achieve a moon orbiting around a planet that is orbiting, how would I do this. I assume I need to somehow get the CFrame of the planet and make that the orgin?

3 Likes

If anyone needs further explanation please let me know!

2 Likes

u have to use trigonometry for it, mainly sin and cos. u first have to convert degrees to radians. then use sin and cos for x and y axis e.g

local radius = 5 --offset

local formula = math.rad(degree)
local x = math.sin(formula)*radius
local z = math.cos(formula)*radius

Part:PivotTo(Origin * CFrame.new(x, 0, z))
2 Likes

So where in the code should I use this, if you don’t mind me asking?

3 Likes

or do I need to put this script in the moon object?

2 Likes

ur code for achieving this would probably be

local alpha = 0
local radius = 5 --offset
local Origin = CFrame.new()

local Part = workspace.Planets.Mercury

while true do
	alpha += .5
	local formula = math.rad(alpha)
	local x = math.sin(formula)*radius
	local z = math.cos(formula)*radius

	Part:PivotTo(Origin * CFrame.new(x, 0, z))
end
2 Likes

Alright, I’ll attempt this. I’ll let you know the results.

2 Likes

Here, I apologize for not elaborating further. Here’s and image describing what I am trying to accomplish. The Bigger Object will go by Mercury while the smaller will go by Moon.


The white line depicts the planetary orbits.

2 Likes

consider Bigger object the Part and Mercury origin and the smaller as Part and Moon origin

3 Likes

So how should I write the code for this, would I put a script inside the moon object?

3 Likes

u have to adjust the code accordingly to ur workspace hierarchy. U can just put the script in serverscriptservice and reference all the objects from there

3 Likes

alright, will do! Thank you for the help

3 Likes

Question, I’m still having a little trouble with this. Would you be able to contact me on discord for easier communication? If you’re not comfortable with this, that’s completely okay!

2 Likes