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
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)
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);
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!