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
8 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.

player = game.Players.LocalPlayer

function Use(u)
    z = script.u:Clone()
    z.Parent = player.Character.Humanoid
    z:Play()
end

play = true

while play do
    if player.Character.Humanoid.Health == 0 then
        play = false
        if player.TeamColor == "Bright Blue" then
            Use(Sound1)
        else
            Use(Sound2)
        end
        play = true
    end
end
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 — 8y

2 answers

Log in to vote
0
Answered by 8 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):

player = game.Players.LocalPlayer

function Use(u)
    local z = script[u]:Clone() --Make it local. It's only used in the function and it can reduce lag
    z.Parent = player.Character.Humanoid
    z:Play()
end

play = true

while play do
    if player.Character.Humanoid.Health == 0 then
        play = false
        if player.TeamColor == "Bright Blue" then
            Use(Sound1.Name)
        else
            Use(Sound2.Name)
        end
        play = true
    end
    wait() --Add a wait() here to prevent the ROBLOX servers from overloading
end

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 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

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

player = game.Players.LocalPlayer

function Use(u)
    local z = script[u]:Clone() --Make it local. It's only used in the function and it can reduce lag
    z.Parent = player.Character.Humanoid
    z:Play()
end

play = true

while play do
    if player.Character.Humanoid.Health == 0 then
        play = false
        if player.TeamColor == BrickColor.new("Bright Blue") then
            Use(Sound1.Name)
        else
            Use(Sound2.Name)
        end
        play = true
    end
    wait() --Add a wait() here to prevent the ROBLOX servers from overloading
end


Answer this question