In a tournament game I have, when a map loads a song will play. The song starts just fine when the map is cloned and put in the workspace, however when the round ends and the map is removed the song continues to play. How can I fix this? I'm still fairly new to Lua.
Here's the script inside the sound.
local song = script.Parent local Map = script.Parent.Parent if map.Parent == game.Workspace then song:Play() else song:Stop() end
Your problem is that that if
check only runs once - when the Script first enters the Workspace.
In order to have the check run again, you're going to have to put it inside of a function, and connect that function to an event. In this case, I suggest the AncestryChanged
event of the Script itself:
local song = script.Parent local Map = script.Parent.Parent function doCheck() if map.Parent == game.Workspace then song:Play() else song:Stop() end end script.AncestryChanged:connect(doCheck) doCheck() -- Because it probably won't run the first time, when the Script first enters the Workspace.