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

How to destroy an object after touch function with debounces?

Asked by 6 years ago
Edited 6 years ago

So I am creating a script for an interactive garden. If the player steps on the soil which contains a seed on top, the seed dissolves. When a player walks on it once again, a sprout slowly appears. I did that part successfully using two debounce variables.

But I want to continue the script. If the player walks across the soil which now contains the sprout, the sprout will dissolve and a flower will bloom in place.

This is the script so far:

01debounce = false
02rebounce = false
03mbounce = false
04script.Parent.Soil.Touched:connect(function()
05 
06    if not debounce then
07 
08        debounce = true
09 
10        for i = 0, 1, 0.1 do
11        script.Parent.Seed.Transparency = i
12        wait (0.1)
13    end
14    wait(20)
15    debounce = false
View all 52 lines...

How come when I use the "Destroy" Instance, it doesn't work at all?

Also, I want to do the for loop to get rid of the sprout, so it can appear to dissolve. But, when I did the for loop with a separate debounce variable, it did not work at all.

2 answers

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

I like your idea. Upvoted. Now, in this kind of script, I would suggest you a situation variable, like...

1local Situation = "Nothing"

When touched is fired, put an if condition and do seeding process.

1if Situation == "Nothing" then
2    --Seeding process
3    Situation = "Seed"
4end

And same for others, but you have to use Elseif for this.

01if Situation == "Nothing" then
02    --Seeding process
03    Situation = "Seed"
04elseif Situation == "Seed" then
05    --Sprouting process? I am not good at flowers
06    Situation = "SproutOff"
07elseif Situation == "SproutOff" then
08    --Sprouting now???
09    Situation = "SproutOn"
10end

And if we get all those things together...

01local Debounce = false
02local Situation = "Nothing"
03 
04script.Parent.Soil.Touched:connect(function()
05    if Debounce == false then
06        Debounce = true
07 
08        if Situation == "Nothing" then
09            --Seeding process
10        elseif Situation == "Seed" then
11            --Sprouting process? I am not good at flowers
12            Situation = "SproutOff"
13        elseif Situation == "SproutOff" then
14            --Sprouting now???
15            Situation = "SproutOn"
16        end
17 
18        Debounce = false
19    end
20end

Please consider adding wait lines inside if statements. Putting them outside would confuse you, I think...

Edit: Use destroy inside function if you want to destroy something.

Did this help you? Then please upvote and accept as an answer. (Please upvote, I need those :'( )

Ad
Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

This is disappointing, superalp beat me to it, oh well, I spent a lot of time on this so I'll share anyways lol

01local debounce = false
02local seedUsed = false
03local sproutGrowing = false
04local flowerBloomed = false
05local goal = {}
06 
07local function TweenPart(part, Time, goal)
08    game:GetService("TweenService"):Create(part, TweenInfo.new(Time), goal):Play()
09end
10 
11script.Parent.Soil.Touched:Connect(function()
12    if not debounce then
13        debounce = true
14 
15        if not seedUsed then
View all 40 lines...

Your system with multiple debounces was going in the right direction, but just made everything confusing, so I made this use variables to help determine which "phase" of growth the plant is in.

Also, I converted your for i = #, #, # loop to animate into TweenService, which makes it look a lot smoother, and removes some math to make it easier.

Answer this question