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

Touched function plays multiple times, is it possible to make it play only once?

Asked by 5 years ago

I have a script here:

It plays like, 50 times when touching it.

Can it be done without using Region3 or Rays?

part.Teleport1Part.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local thing = game.Players.LocalPlayer.PlayerGui.ScreenGui.TeleportBlackScreen
    thing.Visible = true
        repeat
            wait(0.1)
            thing.BackgroundTransparency = thing.BackgroundTransparency - 0.1
        until thing.BackgroundTransparency <= 0
            hit.Parent.Head.CFrame = part.Teleport1Position.CFrame
        repeat
            wait(1)
            thing.BackgroundTransparency = thing.BackgroundTransparency + 0.1
        until thing.BackgroundTransparency >= 1
        thing.Visible = false
    end
    end
end)

2 answers

Log in to vote
0
Answered by
bossay6 62
5 years ago

you can use a debounce.

local deb = false
part.Teleport1Part.Touched:connect(function(hit)
if deb == false then
    deb = true
    if hit.Parent:FindFirstChild("Humanoid") then
        local thing = game.Players.LocalPlayer.PlayerGui.ScreenGui.TeleportBlackScreen
    thing.Visible = true
        repeat
            wait(0.1)
            thing.BackgroundTransparency = thing.BackgroundTransparency - 0.1
        until thing.BackgroundTransparency <= 0
            hit.Parent.Head.CFrame = part.Teleport1Position.CFrame
        repeat
            wait(1)
            thing.BackgroundTransparency = thing.BackgroundTransparency + 0.1
        until thing.BackgroundTransparency >= 1
        thing.Visible = false
    wait(1) -- You can enter here how many seconds you want it to wait for until deb = false
    deb = false
    end
    end
    end
end)


if this doesn't work, then tell me

0
This does indeed work, thank you a lot! I never had a clue such a thing was possible. Not only will this fix my problem, it will also fix a broken huge part of my game. sebse456 13 — 5y
0
no problem! bossay6 62 — 5y
Ad
Log in to vote
3
Answered by
ben0h555 417 Moderation Voter
5 years ago
Edited 5 years ago

Use a debounce value, a value to make sure it doesn't spam the function when the function is playing.

debounce = 0
part.Teleport1Part.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
    debounce = 1
        local thing = game.Players.LocalPlayer.PlayerGui.ScreenGui.TeleportBlackScreen
    thing.Visible = true
        repeat
            wait(0.1)
            thing.BackgroundTransparency = thing.BackgroundTransparency - 0.1
        until thing.BackgroundTransparency <= 0
            hit.Parent.Head.CFrame = part.Teleport1Position.CFrame
        repeat
            wait(1)
            thing.BackgroundTransparency = thing.BackgroundTransparency + 0.1
        until thing.BackgroundTransparency >= 1
        thing.Visible = false
    debounce = 0
    end
    end
end)

This should slow the rate down, and also make sure it doesnt happen when it is already playing, make sure to use debounce values in functions like this.

If you need it to be slower, you add a wait() before it turns debounce to 0, increase the wait() the less you want it to fire.

0
It has made a slight change, but it still plays at least 20 times. sebse456 13 — 5y
0
add a wait() before it turns debounce to 0, that will slow it down. ben0h555 417 — 5y
0
Doesn't quite make a difference sebse456 13 — 5y

Answer this question