Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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

2 comments

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.

0

no problem!

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.

3 comments

0

It has made a slight change, but it still plays at least 20 times.

0

add a wait() before it turns debounce to 0, that will slow it down.

0

Doesn't quite make a difference

This discussion is preserved as it was. New answers, comments, and votes are closed.