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

How do I stop this script from 'over-cloning' ?

Asked by 3 years ago

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)

4 answers

Log in to vote
1
Answered by
blowup999 659 Moderation Voter
3 years ago
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.

0
There's multiple objects with multiple names. How do I make it find those other objects too? squidiskool 208 — 3y
0
If it finds one of the children in the object, then the other cloned objects will be there also, so the script should confirm whether or not it is already on fire and not add more fire if it's already on fire blowup999 659 — 3y
0
I figured it out, but now I get error Unable to cast string to bool. squidiskool 208 — 3y
0
Oh wait, I did fix it, but now I get attempt to index nil with 'Parent' squidiskool 208 — 3y
Ad
Log in to vote
0
Answered by
imKirda 4491 Moderation Voter Community Moderator
3 years ago

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.

Log in to vote
0
Answered by
crueluu 169
3 years ago
Edited 3 years ago

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)
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Hello there. You must use a debounce.

What is a debounce?

A debounce is basically a cooldown. It is used to prevent problems like yours.

How to use it?

  1. Add a variable called debounce.

  2. Add an if statement to the "onTouched" function checking if debounce is false.

  3. If it's false, set the debounce true, wait 1.5 seconds, and then set it back to false.


Recommendations:

  • 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.


Your script with a debounce:

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)

Answer this question