Optimizing my figure eight pattern CFrame rotation script

Hello,

Recently I helped someone out in #help-and-feedback:scripting-support by making a nightclub laser script that rotated an emitter in the pattern of a figure eight.

I went through multiple renditions of the code before I settled on that pattern, so there are some unnecessary vestiges, and the maxRotation variable which was supposed to be configurable really isn’t.

I was more interested in finding a cleaner way to rotate the CFrame in the pattern of a figure eight than anything else. How would you guys do that? :slight_smile:

You can find the code and the model download link on the help thread:

I’ll upload the model here too: Nightclub Laser.rbxm (4.5 KB)

2 Likes

Hello
I have seen your script and have some ways on how to improve it.
First capitalize the :connect to :Connect. Un-capitalized is old and deprecated so don’t use it.
Second change the tick() to os.clock(). tick() is deprecated old and not as good.
Here’s a finalized script:

local RunService = game:GetService("RunService")

local Rotator = script.Parent
local Origin = Rotator.CFrame

local Dampener = 5 -- higher numbers result in lower speed
local MaxRotation = 180
local x,y = 0,0

local function Lerp(a,b,t)
	return (1-t)*a + t*b
end


RunService.Heartbeat:Connect(function(dt)
	local t = (os.clock()/Dampener)%2
	x,y = Lerp(-1,1,t),Lerp(1,-1,t)
	Rotator.CFrame = Origin*CFrame.Angles(math.rad(x*MaxRotation),math.rad(y*MaxRotation),0)
end)