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.
script.Parent.Touched:Connect(OnTouch)
This line will detect if a player hits a part.
function OnTouch()
--your code here
end
script.Parent.Touched:Connect(OnTouch)
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)
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.
Yep! We are done! You can see other OnTouch part examples below:
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)
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)
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)
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)