Easy Written By WoomyLord

OnTouch Events Created on: 01-01-2019

How to make a part do things.

In order for you to make this work, add a script inside your part. Oh, always look if the part is anchored.

After you have a script inside a part, you need to have a function to do this. Here's how:

function OnTouch()

end

"Are we done yet?" No, we're actually not... We need a line that DETECTS a character if the part is touched.

Under "end", add this:

script.Parent.Touched:Connect(OnTouch)

This line will detect if a player hits a part.

Finished Product:

function OnTouch()
--your code here
end

script.Parent.Touched:Connect(OnTouch)

"Hey, my part does things too fast! How could I fix this!?"

That is where "debounces" come in! This will wait for the part do something. This is what debounces look like:

db = false --indicates if the part can do something again if the statement is true

, doesn't have to be local

function OnTouch()
	if db == false then --is it false? if it is, then...
	db = true --debounce is true
	--YOUR CODE	
	wait(1) --it doesn't matter if it is one second, it can be like 0.5 seconds!
	db = false --debounce is false
	end
end

script.Parent.Touched:Connect(OnTouch)

"Other things, like parts, are reacting to it! How could I put an end to it? (no pun intended)"

This time, we need a variable inside the parentheses in the part where it states "function OnTouch()". We can go with "hit", for now.

db = false

function OnTouch(hit) --Don't forget, he called you "hit".
	if hit.Parent:FindFirstChild("Humanoid") then --can't have zombies, robots, or aliens doing things!
	if db == false then
	db = true
	--YOUR CODE HERE
	wait(1)
	db = false
	end
	end
end

script.Parent.Touched:Connect(OnTouch)

The line "if hit.Parent:FindFirstChild("Humanoid") then" statement will ignore parts. The part will only find things if a part or something has a humanoid named "Humanoid" (case sensitive) inside.

"Oh my gosh, are we done!?"

Yep! We are done! You can see other OnTouch part examples below:

Printing

db = false

function OnTouch(hit) 

if hit.Parent:FindFirstChild("Humanoid") then 

if db == false then
	db = true
	print("scoob") --scoobis speaks, via Output

wait(1)
	db = false
	end
	end
end

script.Parent.Touched:Connect(OnTouch)

Disco Floor

db = false

function OnTouch(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
	if db == false then
	db = true
	script.Parent.BrickColor = BrickColor.Random() --disco
	wait(0.5) --thank you wait(0.5), very cool

db = false

	end
	end
end

script.Parent.Touched:Connect(OnTouch)

Play Sound when touched (if Sound is available under part)

db = false

function OnTouch(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
	if db == false then
	db = true
	script.Parent.Sound:Play() 
	wait(script.Parent.Sound.TimeLength + 5) --Instead of wait(1), use the TimeLength of your audio, no need to put the "+ 5" part.
	db = false
	end
	end
end

script.Parent.Touched:Connect(OnTouch)

Deleting the part completely from the Workspace (or completely from the game)

db = false

function OnTouch(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
	if db == false then
	db = true
	script.Parent:Destroy() --bye, thanks for reading :)
	db = false
	end
	end
end

script.Parent.Touched:Connect(OnTouch)

Easy Written By WoomyLord

See WoomyLord's profile on Roblox

Discussion