I am making a script where If you touch a block It plays music and when you leave it the music stops kinda like a sound region or area music. But the problem is that it doesn't stop when the player gets out of the area, meaning that the audio does play but doesn't stop when the player exits the area. This Is the script:
local part = script.Parent local sound = part:FindFirstChild("Sound") local function music(otherPart) local humanoid = otherPart.Parent:FindFirstChild("Humanoid") if humanoid then sound:Play() if not humanoid then sound:Stop() end end end part.Touched:Connect(music)
This is due to it mattering if humanoid is found, rather than seeing if the player stopped touching, you would, and it would stop if a model with no humanoid hit it. You would have to make another function that would be touchended.
local part = script.Parent local sound = part:FindFirstChild("Sound") local function music(otherPart) local humanoid = otherPart.Parent:FindFirstChild("Humanoid") if humanoid then sound:Play() if not humanoid then sound:Stop() end end end local function stopmusic(otherPart) local humanoid = otherPart.Parent:FindFirstChild("Humanoid") if humanoid then sound:Stop() end end part.Touched:Connect(music) part.TouchedEnded:Connect(stopmusic)
This is untested, and i don't know if it's right or wrong.