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

Animated Door On MouseClick?

Is it possible to make a door turn 90 degrees when clicked with mouse and make it go back to its original position after a couple of seconds?

2 answers

1.) Insert A ClickDetector

2.) Create a fixed part to be used as the door's Hinge. Name it 'Hinge' and put it at the side of the door.

3.) Use this script

local door = script.Parent
local Hinge = workspace.Hinge
local detector = door.ClickDetector
local activated = false
local offsetFromHinge = CFrame.new(5,0,0) --This will be the offset from the door's Hinge

detector.MouseClick:Connect(function(player)
        if activated then return end
        activated = true
        door.CFrame = Hinge.CFrame*CFrame.Angles(0, math.pi/2, 0)*offsetFromHinge
        delay(5, function()
               activated = false
               door.CFrame = Hinge.CFrame*offsetFromHinge
        end)
end)

Should also know that the player parameter will return the player that clicked the ClickDetector

EDIT: I'm just going to explain how the code works.

The detector.MouseClick event fires when a player presses the ClickDetector. Line 8 will check if activated is true, if it is then don't we won't bother to run the rest of the code to prevent players from spamming it, else we'll set activated to true and run the rest of the code. CFraming is a way for positioning objects in a 3D workspace, so line 11 will keep the door at its Hinge, rotate it, and offset it. The delay function:

--Format below
void delay(
       double Time,
       function Func
)

The delay function will wait Time seconds, then fire the function Func. The delay function will not yield, or let the code pause, as it's like it will be running in a different script. After 5 seconds, we'll set the activated variable back to false, and reset the door's CFrame to where it was before.

6 comments

0

You should explain how the script works a bit more, he probably has no clue about CFrames or how your script is laid out.

0

ok fine

0

My door exists of many parts and is in the model 'door'. Do I put the Hinge in the workspace or in the model. If I put it in the model it obviously gave me the output error "Hinge is not a valid member of Workspace". If I put it in the workspace I get the error: CFrame is not a valid member of Model

0

The Hinge goes in the workspace. If the door is a model, instead for line 10, put door:SetPrimaryPartCFrame(Hinge.CFrame*CFrame.Angles(0, math.pi/2, 0)offsetFromHinge) and instead at line 13, put door:SetPrimaryPartCFrame(Hinge.CFrameoffsetFromHinge)

0

What should be the Primary part of the model?

0

Normally, it would be the main part of the model.

Yes, you could either use ClickDetectors, or a combination of the Client's Mouse (Mouse.Target or Mouse.Hit) with a RemoteEvent to connect to the server.

Your best option would probably be to turn the door using CFrames.

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