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

How do I play music when my leaderboard stat changes?

Asked by
GunCoo 0
7 years ago
Edited 7 years ago

I am making a checkpoint game and, I would like to have different music playing after the player reaches each checkpoint. I have created this script and placed it in the StaterGui so the music doesn't overlap other players progress. Here is the script so far.. I cannot figure out why this isn't working

local lvl = game.workspace:FindFirstChild("leaderstats")

if lvl.Name == 1 then script.child.1:Play()
else 
    script.child.1:Stop()
end

The child 1 of the script is the Sound (Music) that I want to play. I know I have some dumb mistake in this code so any help would work. Thanks

0
Either way it still worked. Just change the script to fit and mix it with my script and it'll work. EzraNehemiah_TF2 3552 — 7y

3 answers

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

What the problem is

You cannot compare Name which is a string or text value with a number. Either put quotation marks around the 1. Actually, I don't think you're really allowed to name an object a number because the script would get confused. So to be extra safe, change all the names from number values like "1" to named versions like "one".

[Edit]: It errors when an object is named a number and you're trying to code with it. Note that objects can have numbers in their name as long as it's after a letter. So "A1" would work print(A1) but "1A" wouldn't print(workspace.1A).

If you're using something thought something like FindFirstChild or WaitForChild or any other method to find an object from a string, the number placement doesn't matter. But if you're doing it the normal way: workspace.1A, that would error. workspace["1A"] wouldn't.


Final Product

local lvl = workspace:FindFirstChild("leaderstats") 
--ROBLOX is CaSe SeNsItIvE!! If you're doing game.Workspace make sure the W is capitalized, otherwise just get rid of "game." altogether and just make it say "workspace" with a lowercase w.
if lvl.Name == "One" then script.child.One:Play()
else 
    script.child.One:Stop()
end
--Change the names from 1 to One, 2 to Two and etc.

Hope it helps!

0
Ok this did help because that was one problem that I had with my script. Although I still didn't get it to work and that is my fault because I did a bad job explaining this problem. Somehow i need local lvl to findfirstchild "leaderstats" In the player under the players section in the explorer. Then instead of finding lvl.Name , it should be finding lvl.Value because 1 is the stage the player is a GunCoo 0 — 7y
Ad
Log in to vote
0
Answered by 7 years ago
local lvl = game.Workspace:FindFirstChild("leaderstats")

if lvl.Value == 1 then
    script.child.["1"]:Play()
else 
    script.child.["1"]:Stop()
end

Hope it helps, actually, Its level.Value since you make the leader stats change by modifying the number, not the name. hope it helps :)

Log in to vote
0
Answered by 7 years ago

im pretty sure this is why but I may be wrong should be like

local lvl = game.workspace:FindFirstChild("leaderstats") 

if lvl.Value == 1 then 
script.child.["1"]:Play(00) -- 00 should be changed to the id of the song
else 
script.child.["1"]:Stop(00)-- the same id used in the play part of this script 
end 

Answer this question