Help! I have worked about a week on this script, but no progress. I am mostly trying on getting the line
function onClick() Check.Frame.Visible = false end
to work but nothing! I need help getting this script to disappear, but my output keeps saying "Attempt to call a nil value" I even tried making a nil part in the script by changing false to nil and everything. Please help me find out what is up with the nil value what does it mean?!
local play = script.Parent local Frame = game.StarterGui.Check.Frame local Check = game.StarterGui.Check local Click = play.MouseButton1Down:connect(onClick) function onClick() Check.Frame.Visible = false end
Please help this is so frustrating!
Well, you actually need to define the function before calling it. For example, if I tried to do this:
eatFood("apple") function eatFood(food) print("I just ate " .. food) end
That errors:
ServerScriptService.Script:1: attempt to call global 'eatFood' (a nil value)
18:34:10.778 - Stack Begin
18:34:10.778 - Script 'ServerScriptService.Script', Line 1
18:34:10.779 - Stack End
It doesn't actually recognize the function eatFood because it hasn't been defined yet. Scripts are read from top to bottom, so if I want to call a function I need to make sure it already has been defined. Same goes for variables. If I do this:
print(variable) local variable = "Hi"
That will give me an error, and it won't print. The reason it errors 'Attempt to call a nil value' is because the function hasn't been defined yet, and so to Lua doesn't recognize what you're trying to do. It then defines the function as a nil value. Here's your script fixed:
local play = script.Parent local Frame = game.StarterGui.Check.Frame local Check = game.StarterGui.Check function onClick() Check.Frame.Visible = false end local Click = play.MouseButton1Down:connect(onClick)
Hope I helped, and if I did be sure to let me know and accept my answer :)
You're defining a function
, in a variable, so that's why it is causing an error.
These are your new variables
there was really no need to define click as it did nothing anyway.
local play = script.Parent local Frame = game.StarterGui.Check.Frame local Check = game.StarterGui.Check
This is your function
, this just runs whenever a certain requirement is met.
function clicked(onClick) Check.Frame.Visible = false -- Makes the frame invisible. end
This line of code just runs the above function, clicked, whenever the mouse is pressed down on play.
script.Parent.MouseButton1Down:Connect(clicked)
Here's the full code;
local play = script.Parent local Frame = game.StarterGui.Check.Frame local Check = game.StarterGui.Check function clicked(onClick) Check.Frame.Visible = false -- Makes the frame invisible. end script.Parent.MouseButton1Down:Connect(clicked)
Note: From now on, you should get into the habit of using :Connect
as :connect
has since been deprecated.
If I helped, let me know! And don't forget to accept my answer, or if you need further assistance, leave a comment!