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

How do I make a loop out of this?

I now understand this but when I click it the Grass spawns, then I click a button with destroy and it goes away, with Grass still in Lighting. When I click the green button again the Grass won't appear. Can anyone help?

Bool = false

script.Parent.ClickDetector.MouseClick:connect(function()
	if (not Bool)then
	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")
	Bool = true
	end
end)

2 answers

✓ Accepted answer

Your problem is that you're not reactivating your bool switch - or officially known as debounce.

This will cause your script to never pass the condition on line 4.



So to fix? Reactivate your debounce.



local Bool = false

script.Parent.ClickDetector.MouseClick:connect(function()
	if (not Bool)then --You checked if it's false
		Bool = true --Then set it to true, so the event won't re-iterate.
		local grass = game.Lighting.Grass:Clone()
		grass.Parent = workspace
		script.Parent.BrickColor = BrickColor.new("Really black")
		wait(1)
		script.Parent.BrickColor = BrickColor.new("Dark green")
		Bool = false --Reacitvate it!
	end
end)

More info on debounces here.

You have Bool acting as a sort of debounce for this function.

You require that Bool is false (ie not Bool is true) in order for your script to run -- but once you've made on blade, you set Bool to true (and nothing sets it to false later).

I'm guessing what you meant to do was to disable making new ones while it's changing from black to green -- and then enable it again.

enabled = true -- Use a better name than "Bool"

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

If you just want to know if there's already a part named "Grass" in the workspace, there's no need for a debounce at all:

script.Parent.ClickDetector.MouseClick:connect(function()
	if not workspace:FindFirstChild("Grass") then
		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
end)

4 comments

0

Thank you

0

But now when I click it I can click it over and over and more "Grass" will spawn. is there a way to make it work like this without any more grass spawning until it is out of Workspace?

0

You should explain what you want in the first place when you ask questions -- we need to know what you're thinking, not just be given code and told there's a problem.

0

The title is.. sorta accurate. Just incorrect termonology.

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