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

Issue with sound?

Asked by 9 years ago

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

Log in to vote
0
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

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.

1
So, I used this script, and it works just fine in studio mode, however the music doesn't even start in a server. QuillStrike 60 — 9y
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. adark 5487 — 9y
Ad

Answer this question