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

How do I make my script stop the correct sound?

Asked by 5 years ago

I created a script where when you click, it opens a menu with the songs you can play, and when you click a song it plays it and an animation, and when you click stop it should stop the song and animation. When I run a test server with two players, I have them both play a song. Sometimes when I click stop on one player it will stop the other players song or not stop the song at all. Does anyone know why this is happening?

SERVER SCRIPT:

unequipt = game.ReplicatedStorage.Unequpit
stopsong = game.ReplicatedStorage.StopSong                   -------events
choosesong = game.ReplicatedStorage.ChooseSong
songstopped = game.ReplicatedStorage.songstopped

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        if player.TeamColor == BrickColor.new("Really red") then
            f = game.ReplicatedStorage["British Fife"]
            f:Clone().Parent = player.Backpack
            player.PlayerGui:WaitForChild("ScreenGui").Frame.TextButton1.Text = "The British Grenadiers"             ----------sets guis
            player.PlayerGui:WaitForChild("ScreenGui").Frame.TextButton2.Text = "The Girl I Left Behind Me"
            player.PlayerGui:WaitForChild("ScreenGui").Frame.TextButton3.Text = "Lilliburlero"
            player.PlayerGui:WaitForChild("ScreenGui").Frame.TextButton4.Text = "Rule Britannia"
        end
        if player.TeamColor == BrickColor.new("Really blue") then
            f = game.ReplicatedStorage["French Fife"]
            f:Clone().Parent = player.Backpack
            player.PlayerGui:WaitForChild("ScreenGui").Frame.TextButton1.Text = "La Grenadier"
            player.PlayerGui:WaitForChild("ScreenGui").Frame.TextButton2.Text = "La Charge"
            player.PlayerGui:WaitForChild("ScreenGui").Frame.TextButton3.Text = "La Diane"
            player.PlayerGui:WaitForChild("ScreenGui").Frame.TextButton4.Text = "La Pa Cadence"
        end
    end)
end)


choosesong.OnServerEvent:Connect(function(player,animid,songnumber)
    song = Instance.new("Sound")
    if player.TeamColor == BrickColor.new("Really red") then
        song.Parent = player.Character:FindFirstChild("British Fife").Handle
    end
    if player.TeamColor == BrickColor.new("Really blue") then
        song.Parent = player.Character:FindFirstChild("French Fife").Handle
    end
    song.SoundId = "https://www.roblox.com/Asset?ID="..songnumber                                                    -----plays song
    animation = Instance.new("Animation")
    animation.AnimationId = "https://www.roblox.com/Asset?ID="..animid
    trackanimation = game.Workspace[player.Name].Humanoid:LoadAnimation(animation)
    song:Play()
    trackanimation:Play()
    player.Character.Humanoid.WalkSpeed = 8
    song.Ended:Connect(function()
        songstopped:FireClient(player)
        song:Destroy()
        player.Character.Humanoid.WalkSpeed = 16
    end)
end)





stopsong.OnServerEvent:Connect(function(player)
    song:Stop()
    song:Destroy()                     
    trackanimation:Stop()
    player.Character.Humanoid.WalkSpeed = 16
end)




unequipt.OnServerEvent:Connect(function(player)                                                      ------------when u unequipt
    song:Stop()
    song:Destroy()
    trackanimation:Stop()
    player.Character.Humanoid.WalkSpeed = 16
end)

LOCAL SCRIPT:

unequipt = game.ReplicatedStorage.Unequpit --remoteevent
stopsong = game.ReplicatedStorage.StopSong --remoteevent
choosesong = game.ReplicatedStorage.ChooseSong --remoteevent
songstopped = game.ReplicatedStorage.songstopped --remoteevent

player = game.Players.LocalPlayer 


script.Parent.Equipped:Connect(function(mouse) --equipted
    mouse.Button1Down:Connect(function()
        if player.Character:FindFirstChild("British Fife") then
            if not player.PlayerGui:WaitForChild("ScreenGui").Stop.Visible and not player.PlayerGui:WaitForChild("ScreenGui").Frame.Visible then
                player.PlayerGui.ScreenGui.Frame.Visible = true
            end
        end
    end)
end)



script.Parent.Unequipped:Connect(function() --unequipted
    unequipt:FireServer()
    player.PlayerGui:WaitForChild("ScreenGui").Frame.Visible = false
    player.PlayerGui:WaitForChild("ScreenGui").Stop.Visible = false
end)



player.PlayerGui:WaitForChild("ScreenGui").Frame.TextButton1.MouseButton1Click:Connect(function()
    choosesong:FireServer(4054455538,4009405494)
    player.PlayerGui.ScreenGui.Frame.Visible = false
    player.PlayerGui.ScreenGui.Stop.Visible = true
end)
player.PlayerGui:WaitForChild("ScreenGui").Frame.TextButton2.MouseButton1Click:Connect(function()
    choosesong:FireServer(04116459147,4115819618)
    player.PlayerGui.ScreenGui.Frame.Visible = false
    player.PlayerGui.ScreenGui.Stop.Visible = true                                                      -----------songs
end)
player.PlayerGui:WaitForChild("ScreenGui").Frame.TextButton3.MouseButton1Click:Connect(function()
    choosesong:FireServer(04116482197,4115832422)
    player.PlayerGui.ScreenGui.Frame.Visible = false
    player.PlayerGui.ScreenGui.Stop.Visible = true
end)
player.PlayerGui:WaitForChild("ScreenGui").Frame.TextButton4.MouseButton1Click:Connect(function()
    choosesong:FireServer(04116506453,4115835480)
    player.PlayerGui.ScreenGui.Frame.Visible = false
    player.PlayerGui.ScreenGui.Stop.Visible = true
end)


script.Parent.Unequipped:Connect(function()                                                             -----------unequipt
    stopsong:FireServer()
end)            


player.PlayerGui:WaitForChild("ScreenGui").Stop.TextButton.MouseButton1Click:Connect(function()         ------------stop song
    stopsong:FireServer()
    player.PlayerGui:WaitForChild("ScreenGui").Frame.Visible = true
    player.PlayerGui:WaitForChild("ScreenGui").Stop.Visible = false
end)



songstopped.OnClientEvent:Connect(function()                                            -------------------------------------when song is stopped
    player.PlayerGui:WaitForChild("ScreenGui").Stop.Visible = false
    player.PlayerGui:WaitForChild("ScreenGui").Frame.Visible = true
end)

Answer this question