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

Play sound onTouch only works in studio. Why and how can I fix it?

I wrote this script while I was in studio and it worked when I tested it (In studio) but when I tested it out in the actual game it didn't work. Can someone please help me fix this or give me advice on how I can fix it.

Script:

local SP = script.Parent

function onTouch(hit)
	hit.Parent.Humanoid.CoinCollect:Play()
	end
	
	script.Parent.Touched:connect(onTouch)

9 comments

0

What was the error?

0

And what is the script supposed to do?

0

It was supposed to play a sound when you collect a coin.

0

I think.

0

I have no idea how this could not work...

0

do you have it in the right area in the Workspace?

0

There's a Humanoid placed in StarterPlayer, inside the Humanoid there is an Audio. When the player touches the coin (part) it plays the audio inside the Humanoid.

0

The script in question is placed inside the coin.

0

I do things in an abstract way, sorry for the confusion guys.

2 answers

✓ Accepted answer

The Best Answer and I promise u this will help! Click F9 while running the game to see your errors or use output window to see any errors relating to the game. That is how I solve my studio but not in game errors. If this was the best answer, click accept answer. Good Luck with your game!

2 comments

1

Or he could just check the output...

0

yeah same thing XD

If this script is inside the coin, you don't need the audio to play from the Humanoid.

function onTouch(hit)  
script.Parent.Sound:Play()  -- Whatever the audio file is named, "Sound" "Coin" etc.
end 
script.Parent.Touched:connect(onTouch)

3 comments

0

The coins gets deleted when it is collected, so the sound won't play if I do it this way.

0

Add a wait before the coin disappears. :)

0

Touch. Play(). Wait(2). SP:Remove()

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