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

Why does this Sound Script not work?

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

1 comment

0

Line 3, Where is z defined? You didn't define it, or Made a mistake by misplacing 'v' in place of 'u'

2 answers

✓ Accepted answer

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.

1 comment

0

There's another mistake: He didn't define 'Sound1' nor 'Sound2'. Just a heads up. ;)

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


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