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

Why does my output keep saying "Attempt to call a nil value" for my script ?!?!?!

Asked by 7 years ago

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!

0
Why'd you unaccept my answer just to accept one that was just the same? Even though mine was first? awfulszn 394 — 7y

2 answers

Log in to vote
2
Answered by 7 years ago

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 :)

Ad
Log in to vote
0
Answered by
awfulszn 394 Moderation Voter
7 years ago
Edited 7 years ago

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!

0
I believe he also meant to get the GUIs, not from StarterGui, https://scriptinghelpers.org/blog/common-mistakes#PlayerGui OldPalHappy 1477 — 7y
0
It is not safe to rely on the names of child instances remaining constant, as the names of child instances can be changed by developers or by users with the appropriate permissions. If the name of a child instance that your code depends on is changed, your code will break. pingsockv2 40 — 1y

Answer this question