Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I stop Multiple Cloning?

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

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!

4 comments

0

Officially known as a 'debounce'. http://wiki.roblox.com/index.php?title=Debounce

0

I can only do it once but I want to do it over and over again

0

That's because in TurboFusion's code he never reactivated the bool switch.

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.