Hello, I have this script where fire spreads along with other scripts. It's not a virus, but I can make buildings fall apart using my method. I made this script where on touch it clones into the part it touched. But the scripts and the fire get over-cloned and Studio crashes.The scripts and the fire get cloned way too much into a single brick. How do I make it so it would prevent it from over-cloning? I only want it to clone once.
local childs = script.Parent:GetChildren() function onTouched(hit) for i,v in pairs(childs) do local clone = v:Clone() clone.Parent = hit end end script.Parent.Touched:connect(onTouched)
function onTouched(hit) if not hit:FindFirstChild("Fire") then --for i,v in pairs stuff end end
The script checks whether or not a ParticleEmitter named "Fire" is within the part.
Simple, put this line before you clone it.
if hit:FindFirstChild("NameOfThePart") then else bla bla bla
What this does is that it checks if there is no part with that name in hit, this won't help on 100% because person can touch the part with different body parts, to prevent that you can try making the brick clone into only one part but that may not be possible if you for example want to make person on fire, other solution is that you check all the children and do the same thing if it finds the part but idk if that's possible, should be.
Hm.... Perhaps Use Debounce Heres Your Modified Script:
local childs = script.Parent:GetChildren() local debounce function onTouched(hit) if debounce == false then debounce = true for i,v in pairs(childs) do local clone = v:Clone() clone.Parent = hit wait(1) -- set this to how ever you like debounce = false end end end script.Parent.Touched:connect(onTouched)
Hello there. You must use a debounce.
A debounce is basically a cooldown. It is used to prevent problems like yours.
Add a variable called debounce.
Add an if statement to the "onTouched" function checking if debounce is false.
If it's false, set the debounce true, wait 1.5 seconds, and then set it back to false.
Put "local" before defining a function as it makes it local to its own scope to prevent loss of memory.
Use "connect" with an uppercase "c" as "connect" with a lowercase "c" is deprecated. It is never a good idea to use deprecated functions.
local childs = script.Parent:GetChildren() local debounce = false local function onTouched(hit) if not debounce then debounce = true for i, v in pairs(childs) do local clone = v:Clone() clone.Parent = hit end wait(1.5) debounce = false end end script.Parent.Touched:Connect(onTouched)