Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I play music based on team?

So I want to make it so that different music plays depending on different teams. Here is my script.

local EuropeanTheme = game.ServerStorage:WaitForChild("Fantasy Life- Theme of Fantasy Life")
local AmericanTheme = game.ServerStorage:WaitForChild("Fantasy Life - Theme of intrigue")

game.Players.PlayerAdded:Connect(function(Player)
	if Player.TeamColor == "Crimson" then
		AmericanTheme.Playing = true
	end
	if Player.TeamColor == "Bright blue" then
		EuropeanTheme.Playing = true
	end
end)

2 comments

1

its supposed to be AmericanTheme:Play() and EuropeanTheme:Play()

0

Compare the TeamColor to a BrickColor.

2 answers

I have no clue if this works, or not but here is what I edited/errors:

Music Playing Error:

-- on line 6 and 9:
AmericanTheme.Playing = true -- is incorrect
-- use this instead:
AmericanTheme:Play()
-- also make sure the audio properties is looped. (Looped = true)

Team Dectating Error:

if Player.TeamColor = "blah" then -- will not work
-- Use This Instead:
modelname = "bjh"

if game.Players:playerFromCharacter(Player).TeamColor==game.Teams:findFirstChild(modelname).TeamColor then
-- It works better and allows a easy to edit team name.

Final Code:


modelname = "Blah" -- put the american team name here.
modelnameB = "Blah2" -- put the european team name here.

local EuropeanTheme = game.ServerStorage:WaitForChild("Fantasy Life- Theme of Fantasy Life")
local AmericanTheme = game.ServerStorage:WaitForChild("Fantasy Life - Theme of intrigue")

game.Players.PlayerAdded:Connect(function(Player)
    if game.Players:playerFromCharacter(Player).TeamColor==game.Teams:findFirstChild(modelname).TeamColor then
        AmericanTheme:Play()
    end
    if game.Players:playerFromCharacter(Player).TeamColor==game.Teams:findFirstChild(modelnameB).TeamColor then
        EuropeanTheme:Play()
    end
end)

Enjoy, and if this works please accept.

1 comment

0

It doesn't work. Instead of it playing the music, I get an error on line 8 saying: attempt to index a nil value.

I have no idea if this works, but try it.

modelname = "SomethingTeam"
modelnameB = "OtherTeam"

local EuropeanTheme = game.ServerStorage:WaitForChild("Fantasy Life- Theme of Fantasy Life")
local AmericanTheme = game.ServerStorage:WaitForChild("Fantasy Life - Theme of intrigue")

game.Players.PlayerAdded:Connect(function(Player)
	wait(.1)
	if game.Players:FindFirstChild(Player.Name).TeamColor == BrickColor.new("the color") then
		AmericanTheme:Play()
	end
	if game.Players:FindFirstChild(Player.Name).TeamColor == BrickColor.new("the color") then
		EuropeanTheme:Play()
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.