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

Why doesn't this script work properly? {SOLVED}

function Clicked()
	local Button = script.Parent
	local Frame = script.Parent.Parent.Parent  
   	 local a = script.Parent.Parent.AudioIDInsert.Text 
	local b = script.Parent.Parent.Parent.Parent.MusicForPlayers
        wait(0.1)
		b:Pause()
		b.Volume = 1
		wait(0.1)
		b.SoundId="http://www.roblox.com/asset/?id=" .. a
		wait(0.1)
		b:Play()
        wait(0.1)     
        Frame:Destroy()
end
Button.MouseButton1Click:connect(Clicked)

2 answers

✓ Accepted answer

As you run the script, you'll get the error

input:16: attempt to index global 'Button' (a nil value)

Pretty clearly, that means you haven't defined Button. Which is also clear. Button is only defined in the scope of the function Clicked, we need to define it in the global scope.


You should also tab your code properly.


local Button = script.Parent
function Clicked()
	local Frame = script.Parent.Parent.Parent  
	 local a = script.Parent.Parent.AudioIDInsert.Text 
	local b = script.Parent.Parent.Parent.Parent.MusicForPlayers
	wait(0.1)
	b:Pause()
	b.Volume = 1
	wait(0.1)
	b.SoundId="http://www.roblox.com/asset/?id=" .. a
	wait(0.1)
	b:Play()
	wait(0.1)
	Frame:Destroy()
end
Button.MouseButton1Click:connect(Clicked)

1 comment

1

Damn it worked! Your so smart

The listening event doesn't know what button is because you made it a local variable inside the function. Make it a local variable OUTSIDE the function and it should work.

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