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

Sound not stopping when player dies?

Asked by 5 years ago

The player spawns in this room, and when they touch the floor, if the sound is not playing, then the sound will play. When they try to escape by touching the walls, they will die, which I want to cause the sound to stop.

local model  = script.Parent.Parent
local sound = script.Parent

local function onTouched(Part)
    if Part.Parent:FindFirstChild("Humanoid") and sound.IsPlaying == false then
        sound.TimePosition = 0
        sound:Play()
        if sound.IsPlaying == true then
            Part.Parent.Humanoid.Died:connect(function()
                    if sound.isPlaying then
                    sound.Playing = false
                    end
            end)
        end
    end
end

for i, v in pairs(model:GetChildren()) do
    if v:IsA('BasePart') then
        v.Touched:Connect(onTouched)
    end
end
0
an easy way of making it more efficient would be to do sound:Pause() or sound:Stop() theking48989987 2147 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

i explained this all in a codeshare so dont downvote me pls kthx

local Players = game:GetService("Players")
local model = script.Parent.Parent
local sound = script.Parent

for _,v in pairs(model:GetChildren()) do
  if v:IsA("BasePart") then
    v.Touched:Connect(function(part)
      if part.Parent:FindFirstChild("Humanoid") and sound.IsPlaying == false then
         sound.TimePosition = 0
         sound:Play()
      end
    end)
  end
end

Players.PlayerAdded:Connect(function(player)
  local char = player.Character or player.CharacterAdded:Wait()
  local hum = char:WaitForChild("Humanoid")
  hum.Died:Connect(function()
     sound:Stop()
  end)
end)
0
:) PaliKai13 92 — 5y
0
b) Gey4Jesus69 2705 — 5y
Ad

Answer this question