Ad
Log in to vote
0

How to make sound not play when character dies and respawns?

Asked by 6 days ago

How to make it so that when a player has already touched a checkpoint and the sound is played, if they end up dying and respawns, the sound would not be played again??

Every time I reset my character while testing and I respawn on a checkpoint that I have already touched and the sound has already played, the sound plays again.

Here's the script:

local CKsound = workspace.SoundLibrary.CheckpointSound
local Players = game:GetService("Players")
local CharSpawn = {}


Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(Char)
        Char = CharSpawn
        local PlayerLevel = player:WaitForChild("leaderstats").Level
        wait(PlayerLevel)
        local play = false
        local  debounce = false
        local LevelName = script.Parent.Parent.Parent.Parent.Checkpoints["2"]
        print("Online")

        if not CharSpawn[player.Name] and LevelName.Name ~= PlayerLevel.Value then
            CharSpawn[player.Name] = true
            print(player.Name, "has spawned on a different checkpoint")
        end


        script.Parent.Touched:Connect(function(hit)
            for i, parts in ipairs(script.Parent:GetTouchingParts()) do
                if parts == hit.Parent or not hit.Parent:FindFirstChildWhichIsA("Humanoid") then
                    debounce = false
                    print("Touch reset")
                else    
                    if hit.Parent:FindFirstChildWhichIsA("Humanoid") and parts ~= hit.Parent then
                        local Player = Players:GetPlayerFromCharacter(hit.Parent)
                        if Player and debounce == false then
                            debounce = true
                            print("ACTIVE")


                            if Player and play == false then    
                                if LevelName ~= nil then            
                                    print("Part Name is", LevelName, "and Player Level is ", PlayerLevel.Value)


                                    if not CharSpawn[player.Name] and LevelName.Name == PlayerLevel.Value then
                                        play = true                                                 
                                        print(player.Name, "has spawned on same checkpoint")
                                    end


                                    if CharSpawn[player.Name] == true and LevelName.Name == PlayerLevel.Value then
                                        play = true
                                        CKsound:Play()      
                                        print("Checkpoint reached")
                                        print(Player, "has touched Checkpoint", LevelName.Name)
                                    else
                                        if CharSpawn[player.Name] == true and LevelName.Name ~= PlayerLevel.Value then  
                                            play = false    
                                            print("Checkpoint not reached")
                                        end
                                    end


                                    for i, char in ipairs(Players:GetPlayers()) do  
                                        local humanoid = char.Character:WaitForChild("Humanoid")
                                        humanoid.Died:Connect(function()
                                            if play == true and LevelName.Name == PlayerLevel.Value then
                                                print(player.Name, "has died!")
                                                CKsound:Stop()  
                                                print("Checkpoint touched already")
                                            end
                                        end)
                                    end


                                    PlayerLevel:GetPropertyChangedSignal("Value"):Connect(function()
                                        print("Part Name is", LevelName, "and Player Level is ", PlayerLevel.Value) 
                                        if LevelName.Name == PlayerLevel.Value then
                                            play = true
                                            CKsound:Play()      
                                            print("Checkpoint reached")
                                            print(Player, "has touched Checkpoint", LevelName.Name)
                                        else
                                            if LevelName.Name ~= PlayerLevel.Value then 
                                                play = false        
                                                print("Checkpoint not reached") 
                                            end                                             
                                        end                                         
                                    end)                                            
                                end                                     
                            end                             
                        end
                    end                 
                end 
            end 
        end)
    end)
end)


Players.PlayerRemoving:Connect(function(plr)
    table.remove(CharSpawn, table.find(CharSpawn, plr.Name))
    print(plr, "left")
end)



This piece of coding that I added to help fix the issue is not helping either:


for i, char in ipairs(Players:GetPlayers()) do local humanoid = char.Character:WaitForChild("Humanoid") humanoid.Died:Connect(function() if play == true and LevelName.Name == PlayerLevel.Value then print(player.Name, "has died!") CKsound:Stop() print("Checkpoint touched already") end end) end

Answer this question