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

Making my script work multiple times/Repeat?

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)

1 comment

0

Try printing Opened and see what it equals, also it may be because you are spamming the click detector

1 answer

✓ Accepted answer

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.

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