I made a simple door that, if you press the button, the door should lift up. There is a value that states if it is open or closed or not. It doesn't work for some reason. I need to mention 4 things before I show the script:
with that being said here is the script.
local door = script.Parent.Parent.Door local button2 = script.Parent.ClickDetector local open_close = door.Open_Close local sound = script.Parent.Parent.metal_door local function open() sound:Play() door.CFrame = CFrame.new(door.Position + Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position + Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position + Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position + Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position + Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position + Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position + Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position + Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position + Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position + Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position + Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position + Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position + Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position + Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position + Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position + Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position + Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position + Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position + Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position + Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position + Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position + Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position + Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position + Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position + Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position + Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position + Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position + Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position + Vector3.new(0, 0.1, 0)) wait(0.001) end local function close() sound:Play() door.CFrame = CFrame.new(door.Position - Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position - Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position - Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position - Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position - Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position - Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position - Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position - Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position - Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position - Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position - Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position - Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position - Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position - Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position - Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position - Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position - Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position - Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position - Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position - Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position - Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position - Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position - Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position - Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position - Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position - Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position - Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position - Vector3.new(0, 0.1, 0)) wait(0.001) door.CFrame = CFrame.new(door.Position - Vector3.new(0, 0.1, 0)) wait(0.001) end button2.MouseClick:Connect(function() if open_close == false then open_close = true open() elseif open_close == true then open_close = false close() end end)
edit: fixed, thanks to repgainer3. thank you so much.
Hi, frankly I am horrified by this code. But don't worry, we've all been there! What you're missing are loops
, in the case specifically, we're looking for for
loops.
for
loops allow you to run lines of code a specified number of times without needing to type all of them out.
Example:
local Part = workspace.Part for i = 1, 100 do wait(0.1) Part.CFrame = CFrame.new(Part.Position + Vector3.new(0, 0.1, 0)) end
This code will run the wait
and the Part.CFrame =
lines 100 times and then continue with the script.
Furthermore, wait(0.001)
can not be done. If you are looking for the fasted wait possible, you can simply write wait()
with no argument. I believe the lowest by default is 0.03 repeating.
To be exact, your problem is that you're treating the variable open_close
as if it were a boolean
value, or a true or false value, when in reality it is not, it's an object. If the object is a BoolValue
like I assume it is, you'll need to add .Value
to the end to specify it's value.
Also, I would use a debounce here to make sure that both open()
and close()
aren't able to fire at the same time, all it does is makes sure you can't click the door until it's either fully open or fully closed.
local door = script.Parent.Parent.Door local button2 = script.Parent.ClickDetector local open_close = door.Open_Close local sound = script.Parent.Parent.metal_door local door_debounce = false function open() sound:Play() for i = 1, 29 do wait() door.CFrame = CFrame.new(door.Position + Vector3.new(0, 0.1, 0)) end end function close() sound:Play() for i = 1, 29 do wait() door.CFrame = CFrame.new(door.Position - Vector3.new(0, 0.1, 0)) end end button2.MouseClick:Connect(function() if door_debounce then return end door_debounce = true if open_close.Value == true then open_close.Value = false close() else open_close.Value = true open() end wait(1) door_debounce = false end)