game.Workspace.RemoteEvent.OnServerEvent:Connect(function() if game.Players.LocalPlayer.leaderstats.Money.Value >= 20 then game.Lighting.Card:Clone().Parent = game.Players.LocalPlayer.Backpack end end)
i want to make a click detector in my workspace that can acces the local player i made it with a remote event but i get a error
Workspace.RemoteEvent.Script:3: attempt to index field 'LocalPlayer' (a nil value)
how to fix this?
Firstly, I'd like to give an explanation about LocalPlayer and server scripts. Scroll down to "The fix" if you need to. But before I do that, you should know that your question suffers from the XY problem.
You want to get the player from a ClickDetector. You couldn't do that. You thought that through a RemoteEvent, you could get the player through a ClickDetector, and instead of asking about how to get the player through a ClickDetector, you asked how to get the player through a RemoteEvent. Because of that, you have failed to realise you have asked a bad question.
Learn more here, so that you can ask better questions
That aside...
The top ancestor (excluding the DataModel
and the Players
service) of a LocalScript
is typically, the LocalPlayer
. You can also place them in ReplicatedFirst
and have them execute there, but we aren't going to talk about that. 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
OnServerEvent passes the player who fired the remote event as the first argument.
game.Workspace.RemoteEvent.OnServerEvent:Connect(function(player) if player.leaderstats.Money.Value >= 20 then game.Lighting.Card:Clone().Parent = player.Backpack end end)
If you need to use a ClickDetector, you're in luck. MouseClick passes the player who clicked.
local part = ... -- path to part here part.ClickDetector.MouseClick:Connect(function(player) if player.leaderstats.Money.Value >= 20 then game.Lighting.Card:Clone().Parent = player.Backpack end end)
Finally, you should not be using Lighting for storage. Use ReplicatedStorage and/or ServerStorage instead.
You can't use LocalPlayer
in server scripts. Fortunately OnServerEvent
's first parameter is the player that fired the event.
game.Workspace.RemoteEvent.OnServerEvent:Connect(function(plr) plr.leaderstats.Money.Value = 2 --example end)
Note that your remote events should not be in workspace. You should consider moving them to ReplicatedStorage. Additionally, the fact that you asked about a click detector, but I only see a remote event tells me that you used MouseClick
on the client. You should only use this event on the server. It's only parameter is also the player who clicked. Here's an example in a server script:
local part = workspace.Part local click = part.ClickDetector click.MouseClick:Connect(function(plr) print(plr.Name) end)