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
just call the :Stop() function https://developer.roblox.com/api-reference/function/Sound/Stop
an easy way of making it more efficient would be to do sound:Pause() or sound:Stop()