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

How to make lever rotate on clicked?? *like in real life*

So when u click thats what i want it to do rotate it down to act as a clicked but it just cframes somewhere far where i cant even find it ? Why does it do that ? help :/

Open = script.Parent
OpenStick = script.Parent.Parent.Open1

function onClick()
	
	Open.CFrame=CFrame.fromEulerAnglesXYZ(0, 0.45, 0)
	OpenStick.CFrame=CFrame.fromEulerAnglesXYZ(0, 0.45, 0)
end

function onClick()

	Open.CFrame=CFrame.fromEulerAnglesXYZ(0, 0, 0)
	OpenStick.CFrame=CFrame.fromEulerAnglesXYZ(0, 0, 0)
end

script.Parent.ClickDetector.MouseClick:connect(onClick)

1 answer

Concept!

Hi = 5
print(Hi)

Hi = 10
print(Hi)

Output:

5

10

As ROBLOX runs this script, two lines are printed. But why aren't they the same? Or why aren't they both in the same line? That's because variable Hi is defined in line 1, but then it's redefined (or replaced) with another value in line 4.

Same case with your onClick().

function onClick()

    Open.CFrame=CFrame.fromEulerAnglesXYZ(0, 0.45, 0)
    OpenStick.CFrame=CFrame.fromEulerAnglesXYZ(0, 0.45, 0)
end

function onClick()

    Open.CFrame=CFrame.fromEulerAnglesXYZ(0, 0, 0)
    OpenStick.CFrame=CFrame.fromEulerAnglesXYZ(0, 0, 0)
end

-- As the script is running, onClick() will be defined as the second function.

So, use different names. Or combine the two functions into one.

Since you're dealing with a switch/toggle type script (where it's just on and off), I'll give you a sample script to base your work off of.

local isOn = false
-- isOn is false, which means it's off.

local function Switch()
	if isOn then
		print("Turning off...")
	else
		print("Turning on...")
	end
	isOn = not isOn
end

-- Every time this function runs, isOn will be redefined.
local isOn = false

local function TurnOn()
	print("Turning on...")
end

local function TurnOff()
	print("Turning off...")
end

local function Switch()
	if isOn then
		TurnOn()
	else
		TurnOff()
	end
	isOn = not isOn
end

-- More functions. Different way to organize. Same result.

CFrame!

When you're using CFrame.Angles() only, you're making a new CFrame. The default new CFrame is CFrame.new(0, 0, 0). What you want to do is define the CFrame relative to its current CFrame.

Part.CFrame = Part.CFrame * CFrame.fromEulerAnglesXYZ(0, 0, 0)
-- CFrame.Angles is shorter to typppee.. :c

CFrame.fromEulerAnglesXYZ() (or CFrame.Angles() for short) accepts values in radians. Which is, like, trig stuff (math.pi /2 = 90 degrees; math.pi / 4 = 45 degrees), but there's a mathematical function that lets you convert these degree values into radians, if that's easier for you.

Part.CFrame = Part.CFrame * CFrame.Angles(0, math.rad(45), 0)
-- This rotates the part 45 degrees to the side.

To go back to the original CFrame, you can negate the original angle value.

Part.CFrame = Part.CFrame * CFrame.Angles(0, math.rad(-45), 0)

Any questions? Comments? Skepticism? Comment down below or PM me!

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