New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Still have questions? Join our Discord server and get real time help.
Log in to vote

Sound Not Playing When part touched?

Asked by 10 months ago

Hello! I am facing a problem. I already know how to make a sound only play for a specific player. And YES this is a local script, I was wondering why the sound won't play when the part is touched. Also i have collisions off and i spawn at the top of my lobby which is weird. And the LobbyMusicPart is in workspace, and no errors too.

Here is the script...

local LobbyMusicPart = game.Workspace.LobbyMusicPart local playergui = game.Players.LocalPlayer:WaitForChild("PlayerGui") local LobbyMusic ="Sound")

LobbyMusic.Name = "LobbyMusic" LobbyMusic.Parent = playergui LobbyMusic.SoundId = "rbxassetid://2458556355"

if LobbyMusicPart.Touched == true then LobbyMusic.Playing = true else LobbyMusic.Playing = false LobbyMusic.TimePosition = 0 end

Where does this script exist? HeroKajusa 0 — 10mo

3 answers

Log in to vote
Answered by 10 months ago

Sound Problems

So basically, I haven't tested this out yet, but i think you shouldn't make an and instead just directly insert the sound and its properties into the workspace. Also, some sounds are deleted by roblox, try another sound maybe. You use in your "if statement" Playing == true. Use :

|IsPlaying == true|  

Spawn Problems

. About your lobby spawn problems, these problems happen a lot so you could create your own spawning location. I wont write how to write the code so the readers can have a little challenge. However, here are some hints: Use playerAdded, HumanoidRootPart, and CFrame Create a Connect function

I hope you would try my challenge and succeed with the hints I gave you. I also hope that your game would reach the games page some day. Please upvote if you liked how I gave a challenge by giving hints or if you have another problem that I can help with. You are welcome to message me back on here if you are still struggling.

This does not help him at all. IsPlaying is a read-only property and cannot be changed by the scripter. BashCaster 2670 — 10mo
Log in to vote
Answered by 10 months ago

Do you mean:

    LobbyMusic.Playing = true
    LobbyMusic.Playing = false
    LobbyMusic.TimePosition = 0
Log in to vote
Answered by
BashCaster 2670 Moderation Voter Community Moderator
10 months ago

Why not try using the Play() method as well as listening for the Touched event?

   local debounce = false
        if game.Players:GetPlayerFromCharacter(hit.Parent) then
            if debounce == false then
                debounce = true
                if LobbyMusic.TimePosition == 0 then
                elseif LobbyMusic.TimePosition > 0 then
                    if LobbyMusic.Playing then
                        LobbyMusic:Pause() -- Unlike Stop(), Pause() does not reset the TimePosition. It keeps it the same and when Resume() is called the music will play from the TimePosition it was paused at.
                    elseif not LobbyMusic.Playing then
                debounce = false

The above code does many things:

  • If the music is not currently playing and its TimePosition is 0, play the music.
  • If the music's TimePosition is greater than 0 and the music is currently playing, pause the music.
  • If the music's TimePosition is greater than 0 but the music is not currently playing, resume the music.

Unless this is fine with you, you can completely stop and reset the lobby music using the Stop() method.

One other thing is that you should listen for an event to happen rather than the script only checking once to see if it had occurred. This is because the event takes time to fire and if the script checks only once the event will not be seen.

The above script also includes a debounce. Debounce variables serve the purpose of preventing a function from running more than once within a certain amount of time. This is extremely useful if you have events that may involve constant firing, such as the Touched event.

Answer this question