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

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

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)

1 comment

0

it doesnt work cuz u use a mix of deprecated andn non deprecated stuff

2 answers

✓ Accepted answer

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.

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

3 comments

0

But why the else and return part? Does literally nothing.

0

and a function and variable that only gets used once seems pointless to me

0

The question literally asked for it. Coot, read the question first.

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