Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

1 comment

0

script.Parent.Touched:Connect(OnTouch) -- Inside a script in a brick.

1 answer

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.

3 comments

0

Okay, but the music still doesn't play when you touch the brick, am I doing anything wrong with that part?

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

0

@CaptainAlien132 Try doing :Play()

This discussion is preserved as it was. New answers, comments, and votes are closed.