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

Why is my music script playing twice after leaving a music zone?

Asked by 4 years ago

Why is my music script playing twice after leaving a music zone? I dont understand. Here is the script:

function PartToRegion3(obj)
    local abs = math.abs

    local cf = obj.CFrame -- this causes a LuaBridge invocation + heap allocation to create CFrame object - expensive! - but no way around it. we need the cframe
    local size = obj.Size -- this causes a LuaBridge invocation + heap allocation to create Vector3 object - expensive! - but no way around it
    local sx, sy, sz = size.X, size.Y, size.Z -- this causes 3 Lua->C++ invocations

    local x, y, z, R00, R01, R02, R10, R11, R12, R20, R21, R22 = cf:components() -- this causes 1 Lua->C++ invocations and gets all components of cframe in one go, with no allocations

    -- https://zeuxcg.org/2010/10/17/aabb-from-obb-with-component-wise-abs/
    local wsx = 0.5 * (abs(R00) * sx + abs(R01) * sy + abs(R02) * sz) -- this requires 3 Lua->C++ invocations to call abs, but no hash lookups since we cached abs value above; otherwise this is just a bunch of local ops
    local wsy = 0.5 * (abs(R10) * sx + abs(R11) * sy + abs(R12) * sz) -- same
    local wsz = 0.5 * (abs(R20) * sx + abs(R21) * sy + abs(R22) * sz) -- same

    -- just a bunch of local ops
    local minx = x - wsx
    local miny = y - wsy
    local minz = z - wsz

    local maxx = x + wsx
    local maxy = y + wsy
    local maxz = z + wsz

    local minv, maxv = Vector3.new(minx, miny, minz), Vector3.new(maxx, maxy, maxz)
    return Region3.new(minv, maxv)
end

local config = game.ReplicatedStorage.YoshiChillMusicConf
local sounds = config.GlobalMusics
wait(5)
local keep = true
spawn(function()
    while wait() do
        local ins
        for _, p in ipairs(config.Zones:GetChildren()) do
            local r3 = PartToRegion3(p)
            for _, part in pairs(workspace:FindPartsInRegion3(r3, nil, math.huge)) do

                local character = false
                -- In the case the part is a limb
                if part.Parent:FindFirstChild("Humanoid") then 
                    character = true
                end

                if character then

                    character = part.Parent
                    local player = game.Players:GetPlayerFromCharacter(character)
                    if player and player.Name == game.Players.LocalPlayer.Name then
                        --zone music
                        keep = false
                        print('detected inside')
                        ins = true
                        wait(2)
                        for _, s in ipairs(p.Folder:GetChildren()) do
                            local gi = false
                            spawn(function()
                                repeat wait() if keep then s:Stop() return end until s.IsLoaded
                                if  keep then return end
                                print('loaded')
                                if keep then return end
                                s.Looped = false
                                if keep then return end
                                s.Volume = 0
                                if keep then return end 
                                s:Play()
                                if keep then s:Stop() return end
                                game:GetService('TweenService'):Create(s, TweenInfo.new(s.TimeLength/6), {Volume=config.Volume.Value}):Play()
                                print('playing')
                                if keep then s:Stop() return end
                                local target = s.TimeLength - 0.6 * config.Volume.Value - 1
                                repeat wait() if keep then s:Stop() return end until s.TimePosition > target
                                if keep then s:Stop() return end
                                game:GetService('TweenService'):Create(s, TweenInfo.new(s.TimeLength-target), {Volume=0}):Play()
                                if keep then s:Stop() return end
                                wait(3)
                                if keep then s:Stop() return end
                                s:Stop()
                                gi = true
                                if keep then s:Stop() return end
                            end)
                            repeat wait() if keep then break end until gi == true
                            print('ended song')

                        end

                        break

                    end

                    if not ins then keep = true end
                    end
            if not ins then keep = true end
            end
            if not ins then keep = true end

            end
        end

end)
while wait() do



    for _, s in ipairs(sounds:GetChildren()) do
        local g = false
        spawn(function()
            repeat wait() if not keep then s:Stop() g=true return end until s.IsLoaded
            if not keep then return end
            print('loaded')
            if not keep then return end
            s.Looped = false
            if not keep then return end
            s.Volume = 0
            if not keep then return end 
            s:Play()
            if not keep then s:Stop() g=true return end
            game:GetService('TweenService'):Create(s, TweenInfo.new(s.TimeLength/6), {Volume=config.Volume.Value}):Play()
            if not keep then s:Stop() g=true return end
            local target = s.TimeLength - 0.6 * config.Volume.Value - 1
            repeat wait() if not keep then s:Stop() g=true return end until s.TimePosition > target
            if not keep then s:Stop() g=true return end
            game:GetService('TweenService'):Create(s, TweenInfo.new(s.TimeLength-target), {Volume=0}):Play()
            if not keep then s:Stop() g=true return end
            wait(3)
            if not keep then s:Stop() g=true return end
            s:Stop()
            g = true
            print('ended')
            if not keep then s:Stop() g=true return end
        end)
        repeat wait() if not keep then print('unkeep') break end until g == true
        print('ended twice')
    end
end

Any help would be appreciated!

Answer this question