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

How do I make a "Touched" function not take any input until the script stops?

Asked by 9 years ago

Below is a script that lowers a rock into lava, so you die if you stand on it too long, but if you stand on it without jumping off of it, it reacts weird because it sort of resets the script when you touch it again. I am looking for a way to make the touched function not to do anything with the input while the script is running. Thanks in advance for any help!

function touch(hit)
if hit.Parent ~= nil then
local player = game.Players:playerFromCharacter(hit.Parent)
if player ~= nil then
    script.Parent.Parent.first.Transparency = 1
    script.Parent.Parent.first.CanCollide = false
    script.Parent.Parent.second.Transparency = 0
    wait(0.1)
    script.Parent.Parent.second.Transparency = 1
    script.Parent.Parent.second.CanCollide = false
    script.Parent.Parent.third.Transparency = 0
    wait(0.1)
    script.Parent.Parent.third.Transparency = 1
    script.Parent.Parent.third.CanCollide = false
    script.Parent.Parent.fourth.Transparency = 0
    wait(0.1)
    script.Parent.Parent.fourth.Transparency = 1
    script.Parent.Parent.fourth.CanCollide = false
    script.Parent.Parent.fifth.Transparency = 0
    wait(0.1)
    script.Parent.Parent.fifth.Transparency = 1
    script.Parent.Parent.fifth.CanCollide = false
    script.Parent.Parent.sixth.Transparency = 0
    wait(0.1)
    script.Parent.Parent.sixth.Transparency = 1
    script.Parent.Parent.sixth.CanCollide = false
    wait(1) --RESET ROCK--
    script.Parent.Parent.first.Transparency = 0
    script.Parent.Parent.first.CanCollide = true

    script.Parent.Parent.second.CanCollide = true

    script.Parent.Parent.third.CanCollide = true

    script.Parent.Parent.fourth.CanCollide = true

    script.Parent.Parent.fifth.CanCollide = true

    script.Parent.Parent.sixth.CanCollide = true
end end end

script.Parent.Touched:connect(touch)
0
I fixed my code TurboFusion 1821 — 9y

2 answers

Log in to vote
0
Answered by 9 years ago

You'll have to use something called debounce. Debounce is basically using a system to ensure that only 1 input is taken while a program is running. To do this, you would have to use a variable, and check whether the variable is a certain value before running the code, like so:

local Active = true
function Test()
    if Active then --Checks to see if the variable "Active" is true
        Active = false --Sets the variable "Active" to false so the code won't run multiple times at once
        --Code
        Active = true --Sets the variable "Active" to true so the code can run again
    end
end

In your case, you would put the code that you have in your function where it says Code, like so:

local Active = true
function touch(hit)
    if Active then
        Active = false
        if hit.Parent ~= nil then
            local player = game.Players:playerFromCharacter(hit.Parent)
            if player ~= nil then
                script.Parent.Parent.first.Transparency = 1
                script.Parent.Parent.first.CanCollide = false
                script.Parent.Parent.second.Transparency = 0
                wait(0.1)
                script.Parent.Parent.second.Transparency = 1
                script.Parent.Parent.second.CanCollide = false
                script.Parent.Parent.third.Transparency = 0
                wait(0.1)
                script.Parent.Parent.third.Transparency = 1
                script.Parent.Parent.third.CanCollide = false
                script.Parent.Parent.fourth.Transparency = 0
                wait(0.1)
                script.Parent.Parent.fourth.Transparency = 1
                script.Parent.Parent.fourth.CanCollide = false
                script.Parent.Parent.fifth.Transparency = 0
                wait(0.1)
                script.Parent.Parent.fifth.Transparency = 1
                script.Parent.Parent.fifth.CanCollide = false
                script.Parent.Parent.sixth.Transparency = 0
                wait(0.1)
                script.Parent.Parent.sixth.Transparency = 1
                script.Parent.Parent.sixth.CanCollide = false
                wait(1) --RESET ROCK--
                script.Parent.Parent.first.Transparency = 0
                script.Parent.Parent.first.CanCollide = true

                script.Parent.Parent.second.CanCollide = true

                script.Parent.Parent.third.CanCollide = true

                script.Parent.Parent.fourth.CanCollide = true

                script.Parent.Parent.fifth.CanCollide = true

                script.Parent.Parent.sixth.CanCollide = true
            end
        end
        Active = true
    end
end

script.Parent.Touched:connect(touch)

Hope this helped!

0
I tried this, but I got an error saying I have to add a end at line 51, I did, but that didn't work, so I tried line 48, And that also didn't work. I only got an error when I didn't have the extra "end" My_Comment 95 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

just use a flag variable

example

local flag = false

function touch(hit)
    if flag == false then
        flag = true 

        --run code here

        flag = false
    end
end
0
This did not work either, and I go no errors. My_Comment 95 — 9y
0
got* My_Comment 95 — 9y
0
then my advice is print messages to the console to see what is happening when ProfessorSev 220 — 9y

Answer this question