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

How do I stop Multiple Cloning?

Asked by 9 years ago

I want to make a script that spawns in a block named Grass that is in Lighting. But if I click the button multiple times then multiple blocks will spawn, when I just want 1. Here is the script.

script.Parent.ClickDetector.MouseClick:connect(function()
    local grass = game.Lighting.Grass:Clone()
    grass.Parent = game.Workspace
    script.Parent.BrickColor = BrickColor.new("Really black")
    wait(1)
    script.Parent.BrickColor = BrickColor.new("Dark green")
end)

1 answer

Log in to vote
1
Answered by 9 years ago

There are a few methods of doing this, but I'll show you the simplest: Boolean logic

Basically, let the script set a variable to true after the grass has been cloned, and have the script only clone the grass if that variable is false. That way, the grass can only be cloned once. Like so:

local Bool = false
script.Parent.ClickDetector.MouseClick:connect(function()
    if (not Bool) then --(not Bool) means the opposite of Bool. So if Bool is false, (not Bool) will return true and vice versa
        Bool = true --Sets the value of bool to true so that way (not Bool) will return false
        --Cloning code
    end
end)

Hope this helped!

0
Officially known as a 'debounce'. http://wiki.roblox.com/index.php?title=Debounce Goulstem 8144 — 9y
0
I can only do it once but I want to do it over and over again LordZerefu 30 — 9y
0
That's because in TurboFusion's code he never reactivated the bool switch. Goulstem 8144 — 9y
0
If you're only going to run a function once (when it's connected to an event), I recommend disconnecting the event instead of using a boolean value. @LordZerefu, read up on the link Goulstem provided, it gives you the code you need. chess123mate 5873 — 9y
Ad

Answer this question