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)
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 LocalScript
s run for one client, you. Script
s run on the server, which is responsible for connecting the players together in your game.
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 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
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()
.
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
.