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

How do I make an onClicked Function work multiple times?

I'm clicking but it only rotates once and then it's stuck at a 45 degree angle.

local chickenbutt = script.Parent

function onClicked(playerWhoClicked)
	chickenbutt.CFrame = CFrame.new(38, 0.5, 34) * CFrame.Angles(45, math.pi, 0)
end

chickenbutt.ClickDetector.MouseClick:connect(onClicked)

1 answer

✓ Accepted answer

That's because you're redefining the CFrame absolutely, not relatively.

Compare this...

local chickenbutt = script.Parent
-- Nice variable name

function onClicked(playerWhoClicked)
	chickenbutt.CFrame = CFrame.new(38,.5,34) * CFrame.Angles(45, math.pi, 0)
	-- CFrame.Angles assumes its constituents values as radian values, not degree values.
	-- If you want correct angle values, use this: CFrame.Angles(math.rad(45), math.pi, 0)
	-- BTW. math.pi is half of 360°, or 180°.
	-- (math.pi / 4) == math.rad(45)
end

chickenbutt.ClickDetector.MouseClick:connect(onClicked)

With this...

local chickenbutt = script.Parent
chickenbutt.CFrame = CFrame.new(38,.5,34) -- If "chickenbutt" is already on this position, you don't need this line here. 

function onClicked(playerWhoClicked)
	chickenbutt.CFrame = chickenbutt.CFrame * CFrame.Angles(45, math.pi, 0)
end

chickenbutt.ClickDetector.MouseClick:connect(onClicked)

See how it uses its own CFrame every time the onClicked function's fired? That's a good thing, because that's what you want.

Touch it one time, and it rotates the given angle. Touch it another time, and it will use the last CFrame, but apply it to the new one.

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