New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
2

How to destroy an object after touch function with debounces?

Asked by 1 year ago
Edited 1 year 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:

debounce = false
rebounce = false
mbounce = false 
script.Parent.Soil.Touched:connect(function()

    if not debounce then 

        debounce = true

        for i = 0, 1, 0.1 do
        script.Parent.Seed.Transparency = i
        wait (0.1)
    end
    wait(20)
    debounce = false 

    end



if not rebounce then 

rebounce = true

     for i = 1, 0, -0.1 do
        script.Parent.Sprout.Transparency = i
        wait (0.1)
    end
        wait(20)

    rebounce = false


if not mbounce then 
    mbounce = true 
         for i = 0, 1, 0.1 do
        script.Parent.Sprout.Transparency = i
        wait (0.1)

        end
        wait(20)
mbounce = false 
end


end

end)

wait(5)

script.Parent.Sprout:Destroy()

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 1 year ago
Edited 1 year ago

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

local Situation = "Nothing"

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

if Situation == "Nothing" then
    --Seeding process
    Situation = "Seed"
end

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

if Situation == "Nothing" then
    --Seeding process
    Situation = "Seed"
elseif Situation == "Seed" then
    --Sprouting process? I am not good at flowers
    Situation = "SproutOff"
elseif Situation == "SproutOff" then
    --Sprouting now???
    Situation = "SproutOn"
end

And if we get all those things together...

local Debounce = false
local Situation = "Nothing"

script.Parent.Soil.Touched:connect(function()
    if Debounce == false then
        Debounce = true

        if Situation == "Nothing" then
            --Seeding process
        elseif Situation == "Seed" then
            --Sprouting process? I am not good at flowers
            Situation = "SproutOff"
        elseif Situation == "SproutOff" then
            --Sprouting now???
            Situation = "SproutOn"
        end

        Debounce = false
    end
end

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 1 year ago
Edited 1 year 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

local debounce = false
local seedUsed = false
local sproutGrowing = false
local flowerBloomed = false
local goal = {}

local function TweenPart(part, Time, goal)
    game:GetService("TweenService"):Create(part, TweenInfo.new(Time), goal):Play()
end

script.Parent.Soil.Touched:Connect(function()
    if not debounce then
        debounce = true

        if not seedUsed then
            seedUsed = true

            goal.Transparency = 1
            TweenPart(script.Parent.Seed, 1, goal)
            wait(20)
            debounce = false

        elseif seedUsed and not sproutGrowing then
            sproutGrowing = true

            goal.Transparency = 0
            TweenPart(script.Parent.Sprout, 1, goal)
            wait(20)
            debounce = false

        elseif seedUsed and sproutGrowing and not flowerBloomed then
            flowerBloomed = true

            goal.Transparency = 1
            TweenPart(script.Parent.Sprout, 1, goal)
            wait(20)
            debounce = false
        end
    end
end)

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