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

Touched Script help?

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)

1 comment

0

Wow, Shawnyg got 1 thumbs up and got accepted, I got 2 but didn't get accepted. We both got 2 rep!

2 answers

✓ Accepted answer

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!

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)

1 comment

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.

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