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

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

Asked by
painzx3 43
8 years ago

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
0
Try game.Lighting.Tools:FindFirstChild("DMR") == nil. You're looking through Lighting in general, not in the Tools folder/model. funyun 958 — 8y
0
Could you post the code? painzx3 43 — 8y
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" funyun 958 — 8y
0
Why use FindFirstChild? Use WFC unmiss 337 — 8y

2 answers

Log in to vote
1
Answered by 8 years ago

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!

Ad
Log in to vote
-2
Answered by 8 years ago

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
0
If and then are not necessary because in this case you are making a statement. User#4422 0 — 8y
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. funyun 958 — 8y
0
:/ Sorry, new to scripting, just trying to be a help. Not to be teased... @funyun User#4422 0 — 8y

Answer this question