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

How do I make a sound stop playing when you touch a part, but then play another sound?

Okay, so my place has two songs in it. One is in a TV called HALO, and another one is in Workspace, called SONIC (not connected to a part or anything). I want the HALO sound to play when you step onto a brick, and the SONIC sound to stop playing. Bassically I made this script so that HALO only plays inside of a house I made (when you touch the part), and SONIC plays when your outside the house. Here's the script

function onTouch()
if game.Workspace.SONIC:Play() then
	game.Workspace.SONIC:Stop()
	wait(1)
	game.Workspace.TV.Script.HALO:Play()
end
end
game.Workspace.MusicPlay:connect(onTouch)

So why doesn't this work? By the way, MusicPlay is the part that I want all of the scripts and stuff to be triggered by when you step on it.

1 comment

0

By the way, I'm just starting to use if then and while statements, so I'm kinda nooby at scripting. so sorry if none of this makes sense.

1 answer

✓ Accepted answer

Your problem is with the if statement "if game.Workspace.Sonic:Play() then" The :Play() has to be "IsPlaying" And on the connecting part for the onTouch function you did not add the "Touched" part which is very Important. If you're making an onTouched function, and if you want only an NPC to execute this function, I have added that below which is if hit.Parent:FindFirstChild("Humanoid") then -- that searches for the humanoid in the character..

function onTouch(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if game.Workspace.Sonic.IsPlaying == true then
game.Workspace.Sonic:Stop()
wait(1)
game.Workspace.TV.Script.HALO:Play()
end
end
end

game.Workspace.MusicPlay.Touched:connect(onTouch)

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