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

How do I stop Touch events from repeating?

My script is activated when a player steps inside a large part, once a player enters it another part fades away, once the player stops touching it the part fades back in. Everything works pretty good however, whenever a player jumps the first fade happens again. How do I stop it from repeating?

Brick = script.Parent.Parent.Touch
	local player = game.Players.LocalPlayer
	Brick.Touched:connect(function()
		script.Parent.Transparency = .1
		wait(.05)
		script.Parent.Transparency = .2
		wait(.05)
		script.Parent.Transparency = .3
		wait(.05)
		script.Parent.Transparency = .4
		wait(.05)
		script.Parent.Transparency = .5
		wait(.05)
		script.Parent.Transparency = .6
		wait(.05)
		script.Parent.Transparency = .7
		wait(.05)
		script.Parent.Transparency = .8
		wait(.05)
		script.Parent.Transparency = .9
		wait(.05)
		script.Parent.Transparency = 1
		end)
			
			Brick.TouchEnded:Connect(function()
		script.Parent.Transparency = .9
		wait(.05)
		script.Parent.Transparency = .8
		wait(.05)
		script.Parent.Transparency = .7
		wait(.05)
		script.Parent.Transparency = .6
		wait(.05)
		script.Parent.Transparency = .5
		wait(.05)
		script.Parent.Transparency = .4
		wait(.05)
		script.Parent.Transparency = .3
		wait(.05)
		script.Parent.Transparency = .2
		wait(.05)
		script.Parent.Transparency = .1
		wait(.05)
		script.Parent.Transparency = 0
			end)

3 comments

0

fix indentation

0

Use debounce

0

How? Should I move every line to be flat on the left side?

4 answers

✓ Accepted answer

Add a debounce by doing

Debounce = false
Brick = script.Parent.Parent.Touch
	    local player = game.Players.LocalPlayer
    		Brick.Touched:connect(function()
		if debounce == false then
		debounce = true
	        script.Parent.Transparency = .1
	        wait(.05)
	        script.Parent.Transparency = .2
	        wait(.05)
	        script.Parent.Transparency = .3
	        wait(.05)
	        script.Parent.Transparency = .4
		wait(.05)
	        script.Parent.Transparency = .5
	        wait(.05)
	        script.Parent.Transparency = .6
	        wait(.05)
		debounce = false --then at the of whatever you want to happen when it is touched put that along with another end at the bottom

Hopefully this helps you.

You would want to use a debounce, use the script below as an example.

local Part = script.Parent;
local Touching = false;

Part.Touched:connect(function()
if(Touching==false) then
Touching=true
print("hello world!");
--code here for when a player touches it
end
end)
Part.TouchEnded:connect(function()
Touching=false;
end)

Hope this helps and if it does please select this answer as answered! Thank you

local CNX
CNX = script.Parent.Touched:Connect(function(Hit)
	if CNX then
		print(Hit:GetFullName(), Hit.ClassName)

		CNX:Disconnect()
		CNX = nil
	end
end)
script.Disabled = true
script.Disabled = false

--XD

1 comment

0

Where Would I put this in the script?

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