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

I have a error, "attempt to call a nill value" can anyone help?

Asked by 5 years ago

Everytime I click the box it tells me that I tried to call a nill value. I have tried changing the parameters however that didn't help. I have moved the top line below the rest of the script. Can anyone help me?

script.Parent.ClickDetector.MouseClick:connect(onClicked)

function onClicked(player)
    print(player.."Opened the admin panel")
    game.player.PlayerGui.Admin.Frame.Visible = true
end

0
Thanks for your help everyone! Never been in a better comunity :) Colo292 0 — 5y
0
Don't forget to accept the answer you found most helpful! GrandpaScriptin 148 — 5y

3 answers

Log in to vote
0
Answered by
Miniller 562 Moderation Voter
5 years ago
Edited 5 years ago

game.player is not the way to do that.. Use this (after line 04):

local plr = game.Players:FindFirstChild(player.Name) -- you can change plr to anything except player
if plr then -- if player at game.Players (what you tried to use is game.player) exist:
    plr.PlayerGui.Admin.Frame.Visible = true
end

And make sure there is Admin, and frame inside the playergui.

EDIT: Also, that's because you call the function before even the script reads it. So you should write the line1 AFTER the function. (But use what I wrote above, because that's an error too)

0
there is no way that works, your script is wrong DeceptiveCaster 3761 — 5y
0
Why? Miniller 562 — 5y
0
I tested it and it worked Miniller 562 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

This is happening because you are trying to connect the MouseClick event of the click detector to a function that you define after the connection. Therefore it appears as nil when you try to connect the event. To fix this, connect the event after you have defined the function.

The next problem is that you are trying to concatenate the player object on line 4, which is impossible. To fix this you must index the object's Name property, a string, which can be concatenated.

Additionally, You are incorrectly referencing the player on line 5. The MouseClick event of the click detector returns the player that clicked it, and you can simply index their PlayerGui directly from the player object that is passed into the function.

As a side note: players are not direct members of the game. They are members of the Players service, and therefore the way you would reference a player is not game[Player.Name], it is game.Players[player.Name].

As Isaque232 points out, you can not access the contents of the player's PlayerGui from the server, and instead you will have to use a RemoteEvent to send a message to the client so that they can make the Gui visible.

Revised Server Script:

local Remote = script.Parent:WaitForChild("RemoteEvent") -- change this to fit where the remote is
function onClicked(player)
    print(player.Name.."Opened the admin panel")
    Remote:FireClient(player);
end

script.Parent.ClickDetector.MouseClick:Connect(onClicked)

In a local script:

local Remote = workspace.somepart:WaitForChild("RemoteEvent") -- change this to where the remote is
local Player = game.Players.LocalPlayer;

function makeVisible()
    Player.PlayerGui.Admin.Frame.Visible = true;
end

Remote.OnClientEvent:Connect(makeVisible);
0
connect is deprecated, use Connect Miniller 562 — 5y
1
I just copy pasted his code, but I have fixed it. GrandpaScriptin 148 — 5y
0
Also, copy line 1 after the function, because that caues the "attempt to call a nil value" error. Miniller 562 — 5y
0
AND Also, now output says for me: attempt to concatenate local 'player' (a userdata value) - please test the script before writing it there Miniller 562 — 5y
View all comments (2 more)
0
There were more problems with the script than I first thought. I have fixed all of them and given a more in depth explanation. Thank you for pointing this out. GrandpaScriptin 148 — 5y
0
Thanks for your help. Will definatly use this! Colo292 0 — 5y
Log in to vote
0
Answered by
Isaque232 171
5 years ago
Edited 5 years ago

Hello, It seems that you're trying to access the player incorrecly. When a player enters the game, It's instance goes to the service "Players", so technically you'd need to call the service "Players" before accessing the player through the game. You also should use [ ] to access a children of something with a variable. Since the Variable Player that the MouseClick function gave you is an object you'll need to make it a string and getting It's name is the proper way for this issue. So

game:GetService("Players")[Player.Name] --(...)

would be the proper way of getting to the Player through that script.

Sadly also serverscripts can't access the PlayerGui, so if you're trying to access it through a Serverscript It won't work, unless It's a localscript, So I suggest you to either make it a localscript if you're able to, or use remoteevents to change the client's PlayerGui.

Hopefully that helps with your issue, Make sure to accept the answer if it solved it!

0
Thanks for your help. I will try that! Colo292 0 — 5y

Answer this question