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

How do I make a loop out of this? [closed]

Asked by 9 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

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)

Locked by Goulstem and TheeDeathCaster

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

2 answers

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

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.

Ad
Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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)
0
Thank you LordZerefu 30 — 9y
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? LordZerefu 30 — 9y
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. BlueTaslem 18071 — 9y
0
The title is.. sorta accurate. Just incorrect termonology. Goulstem 8144 — 9y