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

Why does this Sound Script not work?

Asked by
Sir_Melio 221 Moderation Voter
9 years ago

This is a local script in StarterGui. When I run this, no error is given and the sounds don't even get copied into the humanoid.

"Sound1" and "Sound2" are sounds and children to the local script below.

01player = game.Players.LocalPlayer
02 
03function Use(u)
04    z = script.u:Clone()
05    z.Parent = player.Character.Humanoid
06    z:Play()
07end
08 
09play = true
10 
11while play do
12    if player.Character.Humanoid.Health == 0 then
13        play = false
14        if player.TeamColor == "Bright Blue" then
15            Use(Sound1)
View all 21 lines...
0
Line 3, Where is z defined? You didn't define it, or Made a mistake by misplacing 'v' in place of 'u' buoyantair 123 — 9y

2 answers

Log in to vote
0
Answered by 9 years ago

You're telling the script to find an object named "u" inside the script. What you want to do instead of using a period, you want to use brackets, which allow you to do things similar to :FindFirstChild(). However, they can break the script if the object doesn't exist. So make sure that object exists! Here's what I did to your script (Note: Brackets take in strings, so I had to modify the code that calls the function):

01player = game.Players.LocalPlayer
02 
03function Use(u)
04    local z = script[u]:Clone() --Make it local. It's only used in the function and it can reduce lag
05    z.Parent = player.Character.Humanoid
06    z:Play()
07end
08 
09play = true
10 
11while play do
12    if player.Character.Humanoid.Health == 0 then
13        play = false
14        if player.TeamColor == "Bright Blue" then
15            Use(Sound1.Name)
View all 22 lines...

Also, one more tip, use tabs to organize your code like I did. Spaces make it more difficult to keep it organized and spot errors.

0
There's another mistake: He didn't define 'Sound1' nor 'Sound2'. Just a heads up. ;) TheeDeathCaster 2368 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

I am just adding on to the last guys. you have to add BrickColor.new("color") for it to work . e.e.

01player = game.Players.LocalPlayer
02 
03function Use(u)
04    local z = script[u]:Clone() --Make it local. It's only used in the function and it can reduce lag
05    z.Parent = player.Character.Humanoid
06    z:Play()
07end
08 
09play = true
10 
11while play do
12    if player.Character.Humanoid.Health == 0 then
13        play = false
14        if player.TeamColor == BrickColor.new("Bright Blue") then
15            Use(Sound1.Name)
View all 22 lines...

Answer this question