#doublefiring :Connect functions

1 messages · Page 1 of 1 (latest)

exotic sleet
#

I did have help on this before but never understood how to actually disconnect the function while still being able to use it - my fix was to use a debounce but i want to actually use :Once but also be able to use it again? if that makes sense?

Server Script - click button to play claw machine

--Variables here
local Price = 10
ClickDetector.MouseClick:Connect(function(player)
    if inUse.Value then
        player.PlayerGui.ClawMachine.ClawInUse.Visible = true
        task.wait(5)
        player.PlayerGui.ClawMachine.ClawInUse.Visible = false
    else
        firepurchasegui:FireClient(player)
    end
end)

Server fires client (firepurchasegui:FireClient(player))

Local Script - Receives FireClient - Stays running continously

firepurchasegui.OnClientEvent:Connect(function(player) --Connect function
    purchase.Visible = true

    --Another connect function causing double firing and keeping the function active
    play.MouseButton1Click:Connect(function()
        firepurchasegui:FireServer()
        purchase.Visible = false
    end)

    quit.MouseButton1Click:Connect(function()
        purchase.Visible = false
    end)
end)

Client fires server (firepurchisegui:FireServer())

Server Script - Receives FireServer - Double fires

firepurchasegui.OnServerEvent:Connect(function(player)
    if player.leaderstats.Coins.Value < Price then
        player.PlayerGui.ClawMachine.PurchaseFail.Visible = true
        task.wait(2)
        player.PlayerGui.ClawMachine.PurchaseFail.Visible = false
    else
        Datamanager.remCoins(player, Price)
        print(Price)
        player.PlayerGui.ClawMachine.PurchaseSuccess.Visible = true
        task.wait(2)
        player.PlayerGui.ClawMachine.PurchaseSuccess.Visible = false
        fireclientgui:FireClient(player)
        inUse.Value = true
    end
end)
radiant condor
#

this double fires?

exotic sleet
#

yes

radiant condor
#

welp

exotic sleet
#

and the function stays active

sterile spireBOT
#

studio** You are now Level 7! **studio

exotic sleet
#

like keeps running using up memory

radiant condor
#
local Connection1, Connection2 = nil, nil
firepurchasegui.OnClientEvent:Connect(function(player) --Connect function
    if Connection1 then Connection1:Disconnect() end
    if Connection2 then Connection2:Disconnect() end
    purchase.Visible = true

    --Another connect function causing double firing and keeping the function active
   Connection1 = play.MouseButton1Click:Connect(function()
        firepurchasegui:FireServer()
        purchase.Visible = false
    end)

    Connection2 = quit.MouseButton1Click:Connect(function()
        purchase.Visible = false
    end)
end)

This is a very lazy way to solve this but it works 🤷‍♂️

exotic sleet
#

damn that was fast bro

#

cheers

radiant condor
#

i just copy pasted and added some stuff

exotic sleet
#

well before i just used a debounce so it wont double fire

#

this is atleast slightly more elegant

#

i can also reuse this because i know i can seperate out the play.MouseButton1Click:Connect functions outside the onclientevent:Connect function and just make the buttons visible but i have other code with the exact same problem where i cannot seperate it out due to requiring certain variables within its scope and not being able to transfer them over without using a remote event which uses by default a :Connect function

#

.
Found it lol this was my old fix

firepurchasegui.OnClientEvent:Connect(function(player)
    purchase.Visible = true
    play.MouseButton1Click:Connect(function()
        if purchaseConnect then return end
        purchaseConnect = true
        print("fire")
        firepurchasegui:FireServer()
        purchase.Visible = false
        task.wait(1)
        purchaseConnect = false
    end)
    quit.MouseButton1Click:Connect(function()
        if purchaseConnect then return end
        purchaseConnect = true
        print("fire")
        purchase.Visible = false
        task.wait(1)
        purchaseConnect = false
    end)
end)
radiant condor
#

👍

exotic sleet
#

thats insane

radiant condor
#

good

#

now make it in the negatives

#

so it givs you free memory usage

exotic sleet