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

Something is wrong with this script?

Asked by 9 years ago

So this script will clone music and place it in a users camera when a map loads. Everything works fine, except when a player dies, the music is cloned again, and now you have the same song playing twice, this repeats itself every time a player dies. In test mode I can see the music multiplying in the camera on a players death. How do I stop this from happening?

cam = game.Workspace.CurrentCamera
mus = game.Workspace.MapValue


if mus.Value == 3 then
    wait()
    m = script.Music
    mt = m:Clone()
    mt.Parent = cam
    mt:Play()
    end


function onStop()
    if mus.Value == 0 then
        wait()
        mz = cam:findFirstChild("Music")
        if mz then
            mz:Stop()
            mz:Destroy()
        end
    end
end

while true do
    onStop()
    wait()
end

1 answer

Log in to vote
0
Answered by
2eggnog 981 Moderation Voter
9 years ago

Just check if the music already exists in the camera before cloning it.

cam = game.Workspace.CurrentCamera
mus = game.Workspace.MapValue


if mus.Value == 3 then
    wait()
    if not cam:FindFirstChild("Music") then --Make sure there is not already a Music object
        m = script.Music
        mt = m:Clone()
        mt.Parent = cam
        mt:Play()
    end
end


function onStop()
    if mus.Value == 0 then
        wait()
        mz = cam:findFirstChild("Music")
        if mz then
            mz:Stop()
            mz:Destroy()
        end
    end
end

while true do
    onStop()
    wait()
end
Ad

Answer this question