Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

rotate and then rotate back script?(cframe)

I'm making a spotlight that goes up into the sky and moves, now I copied a video on how to rotate a door at a hinge but I changed it a bit, but for my sky spotlight I wanted it to rotate a bit, then rotate a bit backwards but I'm not sure how to do 2 events in one script. Any help is very appreciated. :)

local light = game.Workspace.light4.beam
local base = game.Workspace.light4.baseo

local basePos = base.Position
while true do
 	for i = 70,100 do
 		light.CFrame = CFrame.new(basePos) * CFrame.Angles(0,0,math.rad(i))
	wait()
	 for x = 100, 70 do
	light.CFrame = CFrame.new(basePos) * CFrame.Angles(0,0,math.rad(x))
	end
	end

the second "for i" command never works. im a newbie and I'm probably doing something thats common sense to you wrong

2 comments

0

You don't have a "do" at the end of for i - 100, 70 on line 09

0

ah. i recreated this because i removed the second line in the script, just a typo on my part it still didn't work with the "do"

1 answer

✓ Accepted answer

There are a few errors I want to highlight in your script.

The first is not so much an error, but it can lead to them. At the top of your script, instead of using game.Workspace.objectName, try using workspace:WaitForChild("objectName") so you don't end up referencing nil if the object doesn't load in time.

Secondly, I noticed that your ends are not satisfying your loops.

local light4 = workspace:WaitForChild("light4")
local light = light4:WaitForChild("Beam")
local base = light4:WaitForChild("Baseo")

local defaultLightCFrame = CFrame.new(base.Position)

while true do
	for i = 70, 100 do
		light.CFrame = defaultLightCFrame * CFrame.Angles(0, 0, math.rad(i))
		wait()
	end -- missing this end
	for i = 100, 70, -1 do
		light.CFrame = defaultLightCFrame * CFrame.Angles(0, 0, math.rad(i))
		wait() -- you forgot this wait! otherwise the script will run very fast
		-- and seem like it happens instantly.
	end
end

Note how I created the defaultLightCFrame variable before hand, because in your loop you are unnecessarily calling CFrame.new() a bunch and making potentially thousands of new CFrame instances when you could instead just make one before the loop and reference it.

I hope this helps :)

4 comments

0

this helps a ton for glitches i havent found yet, thank you for that a ton! but it still doesn't seem to rotate back and instead jump to the starting pos

0

Sorry - i forgot one thing. The script is fixed now. The issue was that you weren't using wait() for the second for loop.

0

yea. i added the wait the first time i tried to see if it would work then, and it then tried it again just now after you posted this and it still looks to be teleporting back to the first position. im so confused..

0

Oh I also forgot one more thing: the second for loop should have a -1 after because it is decreasing. I added that into the edit as well.

This discussion is preserved as it was. New answers, comments, and votes are closed.