In my obby, when you reach a checkpoint, I want it to play a sound. It'll only play once, not every time someone steps on it. The checkpoints use teams, so I figured that every time the team is changed it'll play the sound. How exactly do I code this?
How do you play a sound when the team is changed?
2 answers
LocalScript in StarterPlayerScripts
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local PlayerGui = Player.PlayerGui
Player.Changed:connect(function(p)
if p == "Team" then
PlayerGui["SoundName"]:Play()
end
end)
And put a 'Sound' in the StarterGui. You can edit the properties of the Sound to how you like them a bit easier that way. Switch out the SoundName in the script to whatever your Sound is named.
Youll have to use an If statement and also a debounce. Both are fairly simple, I shall try to explain although I am not the best teacher.
So Im going to assume you have lots of teams, as obbies usually do. So its simple you can use a touched event and tell it to only fire 1 time. :)
Insert the script into the part, make sure its a script and not a local script.
local SoundSerivce = game:GetSerivce("SoundService")
local Sound = "Your sound here"
local AlreadyTouched = false
if script.Parent.Touched and AlreadyTouched == false then
Sound:clone().Parent = SoundService
SoundService:PlayLocalSound(SoundService:WaitForChild("Sound"))
AlreadyTouched = true
end
So Ill explain now what the whole script does, It uses ROBLOX's soundservice to take your sound and clone it to sound service then it plays it locally which means only the player who touched the part hears it (I think... It should work).
It then sets the debounce to true meaning it cannot play again because alreadytouched needs to be false in order for the script to run.
Hopefully this works. I am not the best scripter but I do try my best :)
2 comments
Do I put the asset id in the "Your sound here" or insert the sound into the brick?
You can put the sound ID into the Your Sound Here
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
Thank you! I got it to work now!