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

How would I stop this event from firing multiple times?

I have this ClickDetector event, that connects to a function, that moves a part. However, when I press the button multiple times, the animation starts all over again. How would I make the ClickDetector work only once?

ClickDetector1.MouseClick:Connect(function()
	local Arrow2X = Arrow2.CFrame.X
	local OldArrow2X = Arrow2.CFrame.X
	local Arrow2TorsoY = Arrow2.CFrame.Y
	local Arrow2TorsoZ = Arrow2.CFrame.Z
	local Arrow2Ori = Arrow2.Orientation
	wait(4.25)
	if 2+2==4 then
		for i = 1,145,1 do
			Arrow2X = Arrow2X + 0.59
			Arrow2.CFrame = CFrame.new(Arrow2X,Arrow2TorsoY,Arrow2TorsoZ)
			Arrow2.Orientation =	Arrow2Ori 
			wait(0.005)
		end
		for i = 1,10 do 
			Arrow2.Transparency = Arrow2.Transparency + 0.1
			wait(0.1)
		end	
		
	end
	
end)

1 answer

✓ Accepted answer

Use the MaxActivationDistance property of the ClickDetector and set it to 0, doing this will make it impossible to click the detector and activate the function. Then, after however long the animation takes you can set it back to the default value.

ClickDetector1.MouseClick:Connect(function()
    ClickDetector1.MaxActivationDistance = 0
    local Arrow2X = Arrow2.CFrame.X
    local OldArrow2X = Arrow2.CFrame.X
    local Arrow2TorsoY = Arrow2.CFrame.Y
    local Arrow2TorsoZ = Arrow2.CFrame.Z
    local Arrow2Ori = Arrow2.Orientation
    wait(4.25)
    if 2+2==4 then
        for i = 1,145,1 do
            Arrow2X = Arrow2X + 0.59
            Arrow2.CFrame = CFrame.new(Arrow2X,Arrow2TorsoY,Arrow2TorsoZ)
            Arrow2.Orientation =    Arrow2Ori 
            wait(0.005)
        end
        for i = 1,10 do 
            Arrow2.Transparency = Arrow2.Transparency + 0.1
            wait(0.1)
        end 

    end

end)

2 comments

0

What if I need to click the button to make a GUI object show up and click again to close the GUI, while playing the animation?

0

You would use a bool variable and if statement to determine if the gui was open or closed.

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