Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Sound not stopping when player dies?

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

2 comments

0

an easy way of making it more efficient would be to do sound:Pause() or sound:Stop()

1 answer

✓ Accepted answer

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)

2 comments

0

:)

0

b)

This discussion is preserved as it was. New answers, comments, and votes are closed.