How do i make this script not reset the music everytime the player jumps?

  • What are you attempting to achieve? (Keep it simple and clear)
    Basically i wrote a script where everytime a player touches a part music plays as long as they’re inside the part’s limits, once they leave the part’s limits the music stops playing (script below).

local bin = script.Parent
local canCollide = true
function onTouched(part)
if canCollide then
script:WaitForChild(“Music”):Play()
end
end
function touchEnded(part)
if canCollide then
canCollide = false
wait(2)
canCollide = true
end
end
bin.Touched:connect(onTouched)
bin.TouchEnded:connect(touchEnded)

  • What is the issue? (Keep it simple and clear - Include screenshots/videos/GIFs if possible)
    Everytime a player jumps the music restarts. (No its not a problem with the part’s limits its all the way below the ground, and all the way into the sky therefore must be an error in the script.

  • What solutions have you tried so far? (Have you searched for solutions through the Roblox Wiki yet?)
    Yes, but no luck finding anything related to this.

1 Like

I’m pretty sure sounds have an IsPlaying property you can check in your onTouched function:

local music = script:WaitForChild("Music")
if canCollide and not music.IsPlaying then
    music:Play()
end
2 Likes

Thank you for the help i’m kind of new into scripting so expect me to make little mistakes like this one :sweat_smile:

1 Like