Tomroblox54321’s Planetary Orbit Module
This script was originally created by Codes Otaku on Youtube.(I highly suggest you check his channel out). I have turned it into an easy to use module script. Just saying he said gave me permission to post it here.This is my first post so tell me if I did anything wrong.
An Example Script:(This is Earth orbiting the Sun)
local ServerScriptService = game:GetService("ServerScriptService")--Getting ServerScriptService
local OrbitModule = require(ServerScriptService:WaitForChild("OrbitModuleFancy"))--Getting the Module
local PlanetOrbiting = game.Workspace.Earth--Getting the Orbiting Planet
local PlanetGettingOrbited = game.Workspace.Sun--Getting the planet that is getting orbited
local TimeToOrbit = 10--Choosing how long it takes to do a full orbit
local NewOrbit = OrbitModule(PlanetOrbiting, PlanetGettingOrbited, TimeToOrbit)--Running the Module
Example Script 2 :(This is the Moon orbiting the Earth)
local ServerScriptService = game:GetService("ServerScriptService")--Getting ServerScriptService
local OrbitModule = require(ServerScriptService:WaitForChild("OrbitModuleFancy"))--Getting the Module
local NewOrbit = OrbitModule(game.Workspace.Moon, game.Workspace.Earth, .4)--Running the Module
You can do PVInstances or numbers instead of variables now but both work perfectly fine!
I’m CodesOtaku, I love this module!
I made an improved version for you, feel free to use it
Module script:
local RunService = game:GetService("RunService")
local CosineMath = math.cos
local SineMath = math.sin
local Atan2Math = math.atan2
local PIMath = math.pi
local TAU = 2*PIMath
return function(PlanetOrbiting : PVInstance, PlanetGettingOrbited : PVInstance, TimeToOrbit : number, NormalOffset : function)
assert(PlanetOrbiting ~= PlanetGettingOrbited, "Cannot orbit itself, PlanetOrbiting and PlanetGettingOrbited should be different")
local DifferenceVector = PlanetOrbiting:GetPivot().Position-PlanetGettingOrbited:GetPivot().Position --Differnce
local Radius = DifferenceVector.Magnitude
local Angle = Atan2Math(DifferenceVector.Y, DifferenceVector.X)--Angle
local HeartbeatConnection
HeartbeatConnection = RunService.Heartbeat:Connect(function(DeltaTime)
-- Disconnect the event if one of the planets got destroyed
if not (PlanetOrbiting or PlanetGettingOrbited) then
HeartbeatConnection:Disconnect()
HeartbeatConnection = nil
end
-- Polar coordinates 2D
local x = Radius*CosineMath(Angle)
local y = Radius*SineMath(Angle)
local z = NormalOffset and NormalOffset(Angle, x, y) or 0
PlanetOrbiting:PivotTo(PlanetGettingOrbited.CFrame*CFrame.new(x, y, z))
Angle += DeltaTime*TAU/TimeToOrbit
end)
-- Return the heartbeat connection, so we can disconnect it if we no longer wants the part to orbit
return HeartbeatConnection
end
Example script:
local A = workspace.A -- Replace this with some part
local B = workspace.B -- Replace this with some another part
local Orbit = require(script.OrbitModule)
local orbitPeriod = 1 -- Seconds it takes to make 1 full turn
local orbit = Orbit(A, B, orbitPeriod) -- Start the orbit
task.wait(5) -- Wait 5 seconds
orbit:Disconnect() -- Cancel the orbit
Okay, so you would want to use your recorder, now record your ingame footage, stop when you’re done, now you can drag the MP4 file to the forum, and it should say “uploading … videoname” it should take a while and there you have a video!
I see. Since the module use the pivot point, you can achieve that easily by changing the position of the pivot of the planet!
But I agree that it would be much cooler to have such crazy features, I’ll try to get my hands dirty when I have enough time.
Otherwise thanks for the great ideas!