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

How to make this script stop repeating itself in this function?

Asked by 6 years ago

So I made a basic script, but when i go in game, it runs the BrickColor changes, and the transparency but then repeats, it starts from the start how do i make it so at the last command just stop?

script.Parent.Touched:connect(function()
    script.Parent.BrickColor = BrickColor.new("Really red")
    wait(2)
    script.Parent.Transparency = 0.6
    wait(3)
    script.Parent.BrickColor = BrickColor.new("Bright blue")
end)

the last brickcolor change that makes it Bright Blue to just stop - line 6

1 answer

Log in to vote
0
Answered by
mattscy 3725 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

It repeats because the Touched event fires multiple times when something collides with that part. You can add a debounce to fix this.

local debounce = true
script.Parent.Touched:Connect(function()
    if debounce then
        debounce = false
        script.Parent.BrickColor = BrickColor.new("Bright red")
        wait(2)
        script.Parent.Transparency = 0.6
        wait(3)
        script.Parent.BrickColor = BrickColor.new("Bright blue")
        debounce = true --remove this line if you only want it to hapen once
    end
end)
Ad

Answer this question