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
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?
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