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

CFrame.Angles script not functioning. How do I make my fan spin?

wait(2)

spin()

function spin()
	
	for initial = 1, 50 do
	
		local xAxis = game.Workspace.Fan
		local rotation = CFrame.Angles(math.rad(10),0,0)
	
		xAxis.Cframe = rotation

	end

end

Code above, I'm not quite sure why it's not operating. I'm fairly new to LUA and would like to find out how to manually step through each line so I can debug future scripts myself. Loop body does not work by itself, not with loop, and not with function call. I just want to make a fan spin, hence I'm just changing the rotational CFrame by 10 degrees. Perhaps I need to call the script somehow?

EDIT: I've just began researching events. I don't necessarily want it to be "triggered" though, I just want it to be running consistently. Perhaps this is not my problem, the search continues.

1 comment

0

If you want it to go constantly just use a "while true do" loop.

3 answers

✓ Accepted answer

Could be wrong but I think you have to do

local xAxis = game.Workspace.Fan
local rotation = CFrame.Angles(math.rad(10),0,0)

xAxis.Cframe = xAxis.CFrame * rotation -- multiply current cframe by rotation for it to rotate, not set it as the rotated CFrame

2 comments

0

This would cause the fan to accelerate exponentially because you are multiplying and vectors do not work like that.

0

It doesn't seem to work, multiplying by rotation. But I found that my "for" loops aren't working, "while true do" works but I want to come out of the loop at some point.

You would want to use Vector3.new to make sure its formatted as a vector. It probably wouldn't spin because the orientation has no changing variable (i in my example). And finally its better to just have the code in one line rather then setting a bunch of variables.


wait(2)

while true do
    for i=1,50 do
        game.Workspace.Fan.Orientation = CFrame.fromEulerAnglesYXZ(Vector3.new(10*i,0,0))
    end
end

Technically you don't need to use fromEulerAnglesYXZ but its a little more universal and makes it easier to understand.

This isn't the most clean way but it should work and is written similarly to your code.

2 comments

0

this overcomplicates things in my opinion, their solution was fine they just didnt do the CFrame operations correctly

0

I found that my "for" loops aren't working whatsoever. I tried just a while true and the fan can run consistently, however I want to break out the loop so I can slow down the fan or something else.

I'll answer it myself. I thought LUA functioned where the conditions for loops counted in seconds. It does not function in seconds. The script would run too fast, and I just wouldn't see it at all. I upped my conditions to take in 500 and decrement to 0, it works. Also, added a little more just to help visually for my fan.

Also, thanks to Benbebop and plagueus for allowing me to try different and more efficient code.

while true do
	
	wait(20)
	
	for i = 0, 500, 1 do
		
		script.Parent.CFrame = script.Parent.CFrame * CFrame.fromEulerAnglesXYZ(math.rad(0.1*i), 0, 0)
		wait()
		
	end
	
	for i = 500, 0, -1 do
		
		script.Parent.CFrame = script.Parent.CFrame * CFrame.fromEulerAnglesXYZ(math.rad(0.1*i), 0, 0)
		wait()
		
	end
	
end

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