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

How can I make this script only activate once even when multiple targets are present?

Asked by 6 years ago
Edited 6 years ago

In a game I am making, I wanted players to be able to be able to withdraw and deposit party members of different types in and out of their group. The withdrawing part is working how I want it to, but the depositing is proving to be a bit of a hassle. Instead of one party member being deposited, all of it's type are deposited instead. How can I make this script only activate once, even though there are multiple members possible. I hope I made that understandable because I don't really know how to describe it. Here is the script:

function onClicked()
    local plr = script.Parent.Parent.Parent.Parent.Parent
    local store = plr.OnionStore.Yellow
    local num = plr.Character.Num
    local max = plr.Character.Max

    if num.Value ~= 0 then
        local b = plr.Character:GetChildren()
            for index, item in pairs(b) do
            if item.Name == "Pikmin" and item.Available.Value == true then

            local list = item:children()
            local temp = nil
            local temp2 = nil
            for x = 1, #list do
                temp2 = list[x]
            print("Worked this far")    
            if (temp2.className == "NumberValue") and (temp2.Name == "Pik") and (temp2.Value == 2) then
                temp = temp2.Parent
                temp.Leaf.Sound.Volume = 0
                temp:Destroy()
                store.Value = store.Value + 1
                num.Value = num.Value - 1
            end
            end
            end
        end
    end
end
script.Parent.MouseButton1Click:connect(onClicked)

0
it doesnt work cuz u use a mix of deprecated andn non deprecated stuff hiimgoodpack 2009 — 6y

2 answers

Log in to vote
0
Answered by
CootKitty 311 Moderation Voter
6 years ago

If this is all the code in a single script, if your code is only going to run once, you could do this:

script.Parent.MouseButton1Click:Wait()

-- continue code here minus the function part

This way you're not making a function and variable that you only use once, and the event listener isn't always waiting for a Click.

Ad
Log in to vote
-1
Answered by
H4X0MSYT 536 Moderation Voter
6 years ago

Debounce. Do this. Simply states a variable thats set to true if the script has allready been run, check if it's true, if it is, don't continue running script.

local Debounce = false
function onClicked()
    if Debounce == false then
        Debounce = true
        -- Your code
    else
        return -- In b4 him accuses me of bullying the engine :c
    end
end
0
But why the else and return part? Does literally nothing. hiimgoodpack 2009 — 6y
0
and a function and variable that only gets used once seems pointless to me CootKitty 311 — 6y
0
The question literally asked for it. Coot, read the question first. H4X0MSYT 536 — 6y

Answer this question