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

Touched Script help?

Asked by 9 years ago

How do i make this touched script so you can only touch it once and you cant touch it anymore?

function onTouched(hit)

    for i=0,1 ,0.1 do

    wait(0.1)

        script.Parent.Transparency = i  
    end
end

script.Parent.Touched:connect(onTouched)
0
Wow, Shawnyg got 1 thumbs up and got accepted, I got 2 but didn't get accepted. We both got 2 rep! EzraNehemiah_TF2 3552 — 9y

2 answers

Log in to vote
2
Answered by 9 years ago

Use a debounce: Here is how it works. There is a debounce variable that could be anything. True or false. We say that if the variable is true, we can do the script. Then we change the debounce.

--Example
script.Parent.Touched:connect(function()
    print("Hi")
    wait(3)
    print("Bye")
end)

Output: Hi Hi Hi Hi Bye Bye Bye Bye


If we use a debounce, It would look like this.

--Example
local debounce = false

script.Parent.Touched:connect(function()
    if not debounce then --If the debounce is false
        debounce = true --Then turn it true so debounce is not false.
        print("Hi")
        wait(3)
        print("Bye")
        debounce = false --Make it usable after 3 seconds.
    end
end)

Output: Hi Bye



Now we add this to your script:

local used = false

function onTouched(hit)
    if not used then
        used = true
        for i=0,1 ,0.1 do

        wait(0.1)

           script.Parent.Transparency = i  
        end
        used = false
    end
end

script.Parent.Touched:connect(onTouched)



Hope it helps!

Ad
Log in to vote
3
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
9 years ago

Well, to make sure they don't touch it while it's running, set up a debounce. You also should check if it's a Player that touched it. To delete the script, use :Destroy() so..

local on = false

function onTouched(hit)
    if hit.Parent and game.Players:GetPlayerFromCharacter(hit.Parent) and on == false then
        on = true
        for i = 0,1,0.1 do
            wait(0.1)
            script.Parent.Transparency = i
        end
        script:Destroy()
    end
end

script.Parent.Touched:connect(onTouched)
0
A good way to allow multiple people to touch it once is add each person's name to a table when they touch it, then check to see if their name is already in that table each time it is touched. Muoshuu 580 — 9y

Answer this question