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 = Instance.new("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
So basically, I haven't tested this out yet, but i think you shouldn't make an Instance.new 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|
. 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.
Do you mean:
LobbyMusicPart.Touched:Connect(function(hit) LobbyMusic.Playing = true else LobbyMusic.Playing = false LobbyMusic.TimePosition = 0 end
Why not try using the Play()
method as well as listening for the Touched
event?
local debounce = false LobbyMusicPart.Touched:Connect(function(hit) if game.Players:GetPlayerFromCharacter(hit.Parent) then if debounce == false then debounce = true if LobbyMusic.TimePosition == 0 then LobbyMusic:Play() 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 LobbyMusic:Resume() end end wait(3) debounce = false end end end)
The above code does many things:
TimePosition
is 0, play the music.TimePosition
is greater than 0 and the music is currently playing, pause the music.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.