Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make a OnTouch function locally play a sound?

Asked by 6 years ago

So, um... I have a script that I was wanting to locally play music when you touch it and stop playing all other sounds, but I don't know how to make it local and the music doesn't play.

function OnTouch (hit)
workspace.Sounds.ambient.stop()
workspace.Sounds.musicambient.stop()
    workspace.Sounds.engine.play()

end

0
script.Parent.Touched:Connect(OnTouch) -- Inside a script in a brick. Axceed_Xlr 380 — 6y

1 answer

Log in to vote
0
Answered by
lukeb50 631 Moderation Voter
6 years ago

A function only ever runs when you call it or connect it to an event. To call a function you would simply do:

OnTouch(item)

in your script and it would run the function. If you want to tie your function to an event you would do:

script.Parent.Touched:Connect(OnTouch)

This tells Roblox that whenever the "Touched" Event of script.Parent fires, call that function.

0
Okay, but the music still doesn't play when you touch the brick, am I doing anything wrong with that part? CaptainAlien132 225 — 6y
0
This is just a guide on functions. You should connect the function to the touch event of the part you want. i.e. workspace.PartNameGoesHere.Touched lukeb50 631 — 6y
0
@CaptainAlien132 Try doing :Play() Axceed_Xlr 380 — 6y
Ad

Answer this question