Audio part script not working

function onTouched(hit)
	Workspace.ForMyGame.Script.Music:Play()
end
wait (0.5)
script.Parent.Touched:connect(onTouched)

what type of script is this because local scripts don’t work in the workspace


script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and Played == false then
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
		local Sound = game.Workspace.Folder.Sounds.BaseSound:Clone()
		Sound.Parent = Player.PlayerGui
		Sound:Play()

		Played = true
		wait(3)
		Played = false
	end
end)

--Here this is the updated code.
--But this still doesn't work. It is sposed to paly a audio once the player is touching the part.

If this is a LocalScript, then the issue is that the script is parented to a part. LocalScripts work in StarterGui, which means you could parent the script to StarterGui, and instead of script.Parent, put workspace.ForMyGame or game.Workspace.ForMyGame.

This is a normal script. Do I change it to a local script and apply these edits?

If you want to just get it to work, yes, you can. But if you still want to use a normal script, I can help you.

okay, how would I get it to work with a normal script?

Cloning the sound after you touch it is just a memory leak in your face. If you want the sound to play locally (e.g. for the player only), make the script a local script. I’ll rewrite your code so it (should) work. (Follow the advice given by others too)

workspace.MyPart.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and (not playing) then
		workspace.Folder.Sounds.BaseSound:Play()
		playing = true
		workspace.Folder.Sounds.BaseSound.Ended:Wait()
		playing = false
	end
end)

For reference, .Ended is an event fired by Sound instances when they finish playing. We can easily wait for this event using :Wait().
not playing is the same as playing == false.
Change workspace.MyPart to the path of the part that you want to listen to.
I advise that you don’t leave instances with their default names as that could lead to “___ is not a member of ___” errors.

1 Like

Thank you for this information. The audio isn’t playing. I do not know why.

Have you set your SoundId? You probably have to use SoundService to play local sounds. Make sure playing is set to false by default.
If there are any related errors in the Output, send them here.


This is how my Workspace looks like

It might be something on Roblox’s end. Try using the Audio Discovery plugin in the “Plugins” tab of your studio. If the audio ID you are using has a checkmark next to it, it means you can use it without any issues. However, if there is an X next to it, it means there is something wrong with it that doesn’t allow it to play correctly.

There’s no error except this one FFlagSelfieViewFeature is not a valid member of Folder “CoreGui.RobloxGui.Modules.Flags”.

My audio isn’t for sale. Maybe that is the reason. I didn’t put my universal game thing in there but it still works if I set it to playing. It doesn’t work if I interact with the part.

playing is underlined yellow. It is near the end of the second line

Where is the LocalScript? Have you tried moving the LocalScript into somewhere like StarterPlayerScripts?

It doesn’t work. I put it there.

The local script is inside a part which is in a folder.

If you hover over it, it’ll tell you what’s wrong. Describing the colour to me is very vague, but I assume it’s because I’ve changed the name of the variable (Played to playing), so it doesn’t recognise it.

LocalScripts do not work in workspace nor the descendants of workspace, which means you have to put the LocalScript in StarterGui, or another place listed in the image below:

2 Likes