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

How do you fix the error 'attempt to index upvalue 'player' (a nil value)"?

Asked by 5 years ago
Edited 5 years ago

I have been working on a script that worked 2 months ago. It has now since stopped working and has been giving the error " attempt to index upvalue 'player' (a nil value)" My script:

local player = game.Players.LocalPlayer

script.Parent.ClickDetector.MouseClick:connect(function(Clicker)
    if player:GetRankInGroup(3863824)== 254 then 
        local clone = script.Parent.Gui:Clone()
    clone.Parent = Clicker.PlayerGui
    end
end)

0
Is this a regular script? If so it’s because it doesn’t know LocalPlayer, that should be for local script purposes. ABK2017 406 — 5y
0
Are you using a server script again? And I am using Clicker because MouseClick passes the player who clicked the click detector. It can be named anything you want, and you have named it clicker so I used that. User#19524 175 — 5y
0
No, I am using a local script. Henryisnotawesome2 2 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

Ancestry

The top ancestor (excluding the DataModel and the Players service) of a LocalScript is typically the LocalPlayer. You could also place them in ReplicatedFirst but we aren't going to talk about that. And LocalScripts run for one client, you. Scripts run on the server, which is responsible for connecting the players together in your game.

Why LocalPlayer is nil on the server

Think of it as your school classmates and your teacher. Think of your school classmates as other clients, and think of your teacher as the server. If you told your teacher to get the local student, your teacher would be very confused, because which student is the local student? For this reason, LocalPlayer is nil on the server. Your game could have multiple players in it, and when you try using LocalPlayer, it can't just randomly pick a player. It makes no sense.

Code doesn't read minds

Code cannot read your mind. This is why it must be all typed out. So instead of doing this:

make a part inside of the workspace with color red

You must do this:

local part = Instance.new("Part")
part.BrickColor = BrickColor.new("Really red")
part.Parent = workspace 

Fix

With that in mind, let's fix your script. Since you are using MouseClick with a ClickDetector, this will be easy. The MouseClick event gives us the Player who clicked as a parameter, and you have named it Clicker, so let's use that.

script.Parent.ClickDetector.MouseClick:Connect(function(Clicker)
    if Clicker:GetRankInGroup(3863824)== 254 then 
        local clone = script.Parent.Gui:Clone()
        clone.Parent = Clicker.PlayerGui
    end
end)

And finally, Event:connect() is deprecated, use Event:Connect().

0
Hi, Thank you for the help. The script doesn't work somehow. I am also confused by you using clicker in "If Clicker:GetRankInGroup(3863824) == 254 then" Henryisnotawesome2 2 — 5y
Ad
Log in to vote
1
Answered by
RayCurse 1518 Moderation Voter
5 years ago
Edited 5 years ago

The LocalPlayer property of Players can only be accessed from the client. The reason your script may have been working 2 months ago is because accurate play solo wasn't set to on by default (your script would never have worked on live servers). Make sure that this code is running in a LocalScript.

0
Putting the script in a Local script breaks it. Henryisnotawesome2 2 — 5y
0
It's not as simple as copying and pasting it in a local script. Read incapaz's answer as it explains more in depth. RayCurse 1518 — 5y
0
Oh Thanks! Henryisnotawesome2 2 — 5y

Answer this question