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.
Checking if character touched and the hit parameter
If you wanted to check if a character touched this part then you would do an if statement checking if the part's parent has a child named Humanoid in it. Which every character in Roblox has.
local Part = script.Parent --Part directory Part.Touched:Connect(function(hit) --hit parameter if hit.Parent:FindFirstChild("Humanoid") then --If humanoid is found print("Character Found: "..hit.Parent.Name) end end)
So basically hit is the object that touched the part that you assigned the event to. While on the 4th line we are checking if the Humanoid exists. FindFirstChild either returns a bool or in some cases the object found. Then on the 5th line I am printing "Character Found:" with the player's name concatenated to it and that would print "Character Found: uhi_o" (if it were me of course).
Disconnecting an event
Disconnecting an event is used when you won't be needing the event anymore and just disconnect it. I'm not too sure of a case you would need this but I'll try my best to explain it.
local part = script.Parent
local Event = part.Touched:Connect(function(hit)
if hit.Parent:FindFIrstChild("Humanoid") then hit.Parent.Humanoid:TakeDamage(10) --Damaging character touched Event:Disonnect() --Ending this whole touched event end end)
In this example, we first define the local variable “connection” so that it can be used inside the function onTouched. We set the connection variable to be the value returned from Connect (the returned connection object). The onTouched function will print the object that touched the part, then immediately disconnect the function from the event by calling Disconnect on the connection object. This prevents future touches from calling onTouched -Developer roblox
In other words we define the variable of the event then we disconnect it whenever needed.
Thanks for reading! Hope you learnt something! -uhi_o