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

Issue with sound?

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

1 answer

✓ Accepted answer

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.

2 comments

1

So, I used this script, and it works just fine in studio mode, however the music doesn't even start in a server.

0

Search the wiki for the Preload method. Use it to preload the song, so that it will play the first time you Play() it, rather than load.

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