Easy Written By vikenmanoukian21

Touched and TouchEnded Created on: 22-10-2019

On Touch event

On touch event is often used. I will teach you guys how it works. There is another way to do it but i prefer this short and simple method.

Add 2 parts in your game. One button and another bridge for the player to pass ontop. Make sure the button is not touching anything or else it will activate the Touched event.

local part = script.Parent
local part2 = game.Workspace.SecondPart

Now we start the Touched events.


button.Touched:Connect(function()
	print("Button has been touched.")
end

If you do not like using this method then there is another way.


yourPart = script.Parent

function onTouch()
	print("A player has touched the button")
end

yourPart.Touched:Connect(onTouch) --Calls the function

When something touches the button it prints "Button has been touched." in the output. You may change the print to anything you would like it to do. Now for when the player is not touching it.

button.TouchEnded:Connect(function()
	print("Button has been disactivated")
end

When player is not touching the part anymore it de activates. Here is an example of what you can do with Touched events.

local button = script.Parent
local bridge = game.Workspace.Part

button.Touched:Connect(function()
	button.BrickColor = BrickColor.new("Bright Green")
	bridge.Transparency = 0
	bridge.CanCollide = true
end

button.TouchEnded:Connect(function()
	wait(5) -- Time before script starts (seconds)
	button.BrickColor = BrickColor.new("Really red")
	bridge.Transparency = 1
	bridge.CanCollide = false
end

What this script does is. When a player touches the button it becomes red and the bridge comes. Then when nothing is touching it, it waits 5 seconds and it runs the script after it.

Easy Written By vikenmanoukian21

See vikenmanoukian21's profile on Roblox

Discussion