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

Making my script work multiple times/Repeat?

Asked by 7 years ago

So I made this script with my door. It opens nice, and it closes nice. Tho after doing each thing once I can't do it anymore. I believe I need it to repeat or something but have no idea how to do it on the script. Any help is appreciated :)

--//Variables
local Door = script.Parent.Parent
local Main = Door.Main
local Button = script.Parent
local DoorOpen = Door.Sounds:WaitForChild("DoorOpen")


--//Opening and Closing
local Opened = false
Button.ClickDetector.MouseClick:connect(function()
    if Opened == false then
        Opened = true
        DoorOpen:Play()
        for i = 0,3,.1 do
            Door:SetPrimaryPartCFrame(Door:GetPrimaryPartCFrame() * CFrame.Angles(0,math.rad(math.pi/1),0))
            wait()
        end
    end
end)
wait(3)
local Opened = true
Button.ClickDetector.MouseClick:connect(function()
    if Opened == true then
        Opened = false
        DoorOpen:Play()
        for i = 0,3,.1 do
            Door:SetPrimaryPartCFrame(Door:GetPrimaryPartCFrame() * CFrame.Angles(0,-math.rad(math.pi/1),0))
            wait()
        end
    end
end)

0
Try printing Opened and see what it equals, also it may be because you are spamming the click detector User#7446 0 — 7y

1 answer

Log in to vote
0
Answered by
65225 80
7 years ago

The problem is with the Opened variables. You're creating two variables with the same name, so when called, lua will not know which to choose and end. The solution is very easy, just make one variable instead of two.

local Door = script.Parent.Parent
local Button = script.Parent
local DoorOpen = Door.Sounds:WaitForChild("DoorOpen")
local Debounce = true
local Opened = false


Button.ClickDetector.MouseClick:connect(function()
    if not Opened and Debounce then
        Opened = true
        Debounce = false
        DoorOpen:Play()
        for i = 0,3,.1 do
            Door:SetPrimaryPartCFrame(Door:GetPrimaryPartCFrame() * CFrame.Angles(0,math.rad(math.pi/1),0))
            wait()
        end
        Debounce = true
    end
end)
wait(3)
Button.ClickDetector.MouseClick:connect(function()
    if Opened and Debounce then
        Opened = false
        Debounce = false
        DoorOpen:Play()
        for i = 0,3,.1 do
            Door:SetPrimaryPartCFrame(Door:GetPrimaryPartCFrame() * CFrame.Angles(0,-math.rad(math.pi/1),0))
            wait()
        end
        Debounce = true
    end
end)

Also, I added a debounce to prevent spam clicking. Always handy to have.

Ad

Answer this question