Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make an onClicked Function work multiple times?

Asked by 8 years ago

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

Log in to vote
0
Answered by
Redbullusa 1580 Moderation Voter
8 years ago

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.

Ad

Answer this question