Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you play a sound when the team is changed?

Asked by 6 years ago

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?

2 answers

Log in to vote
0
Answered by
c0des 207 Moderation Voter
6 years ago

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.

0
Thank you! I got it to work now! Ender_Derp -3 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

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 :)

0
Do I put the asset id in the "Your sound here" or insert the sound into the brick? Ender_Derp -3 — 6y
0
You can put the sound ID into the Your Sound Here GottaHaveAFunTime 218 — 6y

Answer this question