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

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

Asked by
EpicLilC 150
9 years ago

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.

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. EpicLilC 150 — 9y

1 answer

Log in to vote
1
Answered by 9 years ago

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)
Ad

Answer this question