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

How do I run a chunk of code once? [Unanswered]

I have a crate where upon touching it opens up a GUI which shows your backpack items and the items in the crate for you to take or deposit into. However, I have a certain chunk of code that checks the ReplicatedStorage so that it will move not CLONE but move an item into the chestItems. Works like a charm the first time but once a player comes to open up the chest an error says that DMR is not a valid member of ReplicatedStorage. So how do I run that code ONCE only?

if game.Lighting:findFirstChild("DMR")== nil then
	local weapon = bin.DMR -- Ignore this bin.DMR:clone()
	weapon.Parent = game.Lighting.Tools
	end

4 comments

0

Try game.Lighting.Tools:FindFirstChild("DMR") == nil. You're looking through Lighting in general, not in the Tools folder/model.

0

Could you post the code?

0

Wish I could but I'm on my phone. Just change "if game.Lighting:findFirstChild("DMR") == nil then" with "if game.Lighting.Tools:findFirstChild("DMR") == nil then"

0

Why use FindFirstChild? Use WFC

2 answers

✓ Accepted answer

#What to do

  • Add a debounce of some sort

#Debounce Debounce is a way to make sure a code runs once and then can only be activated again after some time.

local debounce = false

script.Parent.Touched:connect(function()
	print("Touched.")
	if not debounce then
		debounce = true
		print("Touched+Debounce")
		wait(5) --After 5 seconds you can activate this chunk again.
		debounce = false
	end
end

This would be the output:

Touched. Touched+Debounce Touched. Touched. Touched. Touched. Touched. Touched.

You noticed how "Touched+Debounce" was only said once because of the debounce?



#Final Product

local debounce = false
if game.Lighting:FindFirstChild("DMR") == nil and not debounce then
	debounce = true
    local weapon = bin.DMR -- Ignore this bin.DMR:clone()
    weapon.Parent = game.Lighting.Tools
end



Hope it helps!

Don't give me the credit, just posting his answer in code, and it looks correct to me, so try that.

Game.Lighting.Tools:FindFirstChild("DMR") == nil
	local weapon = bin.DMR
	weapon.Parent = game.Lighting.Tools
	end

3 comments

0

If and then are not necessary because in this case you are making a statement.

0

Not quite. He's looking for the DMR in the tools, and if he doesn't find it, he's putting one in there. Purty simple.

0

:/ Sorry, new to scripting, just trying to be a help. Not to be teased... @funyun

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