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 a treasure chest/box flip open relative to a hinge?

Initially, my script looked like:

function onClicked(PlayerWhoClicked)
   
script.Parent.Lid.CFrame =  script.Parent.Lid.CFrame * CFrame.Angles(math.rad(130), 0,0)

end
script.Parent.ClickDetector.MouseClick:connect(onClicked)

The box "opened", however, it only rotated along it's own axis. I want the lid to rotate relative to a hinge connecting to the box.

And one more problem with that script, was that it rotated stud by stud every time you clicked it, and never stops. I want it to flip open to a certain point with one click, and thats it.

So I wrote this new script:

local hinge = workspace:WaitForChild("Hinge")
local lid = script.Parent:WaitForChild("Lid")

hingePos = hinge.Position


function onClicked(PlayerWhoClicked)
	
	for i = 1,180 do
	lid.CFrame = CFrame.new(hingePos)* CFrame.Angles(math.rad(i),0,0)* CFrame.new(14.195,0,0)
	
wait()

end

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

I click, and nothing happens. What went wrong?

1 answer

So I have a script using almost exactly what you want. Group the Lid and the Hinge parts in a model, as well as the part you click to open it. Inside the button put a click detector, two boolvalues, named Open and Moving, and a regular script. Set both bool values to false.

In your function make two if statements, one opening and one closing. Each is opposite to the other.

Ex. "if Open.Value = false and Moving.Value = false then

if debounce then return end
debounce = true

Lid:SetPrimaryPartCFrame(hinge.CFrame*CFrame.Angles(0.01,0,0))

debounce = false
Open.Value = true

Your closing statement will be very similar, replace all trues with false and vice versa, except the debounces, which can stay the same. Make your CFrame angles negative, and set open.Value to false.

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