Edit: I found the Solution but how can i Disconnect the Function a()?
MouseButton1Click will run one Time when i click it but just when “elseif bFrame.Visible == true then…” is not fired. But after a() is fired another Time MouseButton1Click will run two Times and it will be more if i repead it.
local lockBackButton = false
local backButton = ""
local aFrame = ""
local bFrame = ""
local cFrame = ""
local buttonA = ""
local buttonB = ""
function a()
aFrame.Visible = true
backButton.Visible = true
backButton.MouseButton1Click:Connect(function()
if lockBackButton == false then
lockBackButton = true
if aFrame.Visible == true then
aFrame.Visible = false
bFrame.Visible = true
lockBackButton = false
elseif bFrame.Visible == true then
backButton.Visible = false
cFrame.Visible = false
aFrame.Visible = false
task.wait(1)
--More other Code
lockBackButton = false
end
end
end)
end
a()
buttonA.MouseButton1Click:Connect(function()
aFrame.Visible = false
bFrame.Visible = true
end)
buttonB.MouseButton1Click:Connect(function()
bFrame.Visible = false
cFrame.Visible = false
end)
local connection
connection = function a()
-- blah blah the code inside function a()
--when you want to disconnect do this:
connection:Disconnect()
end
Basically, you set your function equal to a variable so its connection can later be disconnected to prevent lag.
Let me know if it works.
local b
local function a()
script.Parent.MouseButton1Click:Connect(function()
print("1")
b:Disconnect()
end)
end
wait(5)
b = script.Parent:WaitForChild("TextButton").MouseButton1Click:Connect(a)
wait(5)
b = script.Parent:WaitForChild("TextButton").MouseButton1Click:Connect(a)
I did this, that also dont works, there are two “b = script.Parent:WaitForChild(“TextButton”).MouseButton1Click:Connect(a)” so i can see how the MouseButton1Click is firing twice
you can do this to disconnect the mousebutton1click event:
local function a()
local Connection : RBXScriptConnection? = nil -- you can ignore "RBXScriptConnection", i just wanted to add type checking lol
Connection = script.Parent.MouseButton1Click:Connect(function()
print("Clicked")
Connection:Disconnect()
end)
end
a()
It’s important that you define the variable “Connection” before actually setting it, else you won’t be able to disconnect it.