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

How do i stop a function from working?

Asked by 2 years ago

So my initial thought was using return but it only stops the if question (I think, not sure), not the function itself.

here is a part of my code that i'm having trouble with.

        Progress:GetPropertyChangedSignal("Value"):Connect(function() -- whenever progress changes
            if Progress.Value >= MaxProgress.Value then -- check if progress already reached the max progress cap required to finish
                PLN.Value = PLN.Value + goldreward.Value -- rewarding
                XP.Value = XP.Value + xpreward.Value
                plr.leaderstats.QuestsDone.Value = plr.leaderstats.QuestsDone.Value + 1 -- adding one quest to quests done
                print(plr.leaderstats.QuestsDone.Value + 1, plr.leaderstats.QuestsDone.Value)
                -- clearing the values
                xpreward.Value = 0
                goldreward.Value = 0
                Progress.Value = 0
                QuestID.Value = ""
                MaxProgress.Value = 0
                gui.Title.Text = texttitle
                gui.Subtitle.Text = textsubtitle
                return
            end
        end)

When i call this function again it just doubles the outcome, I deducted it's because the function didn't stop in the first place.

1
A return is unnecessary if it is the end of the function. COUNTYL1MITS 312 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago

The :Connect function returns a connection that you can call :Disconnect from to stop the function from firing in the future. However, the best use of Connect is to call it once per thing that's going to be changing, not every time you change it. If you're able to run the code every time you change the value, you should simply remove the part where you make a connection and simply run the function yourself.

If you need this to be a connection because other code can change the progress, you may want to store the connections in a table, like this:

-- Top of the script
local connections = {}

-- In your code
connections[plr] = Progress:GetPropertyChangedSignal("Value"):Connect(function()
    -- etc
end)

game.Players.PlayerRemoving:Connect(function(plr)
    connections[plr]:Disconnect()
    connections[plr] = nil
    -- Saving code and other cleanup could go here
end)
Ad

Answer this question