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

how to acces a local player via a clickdetector?

Asked by
hokyboy 270 Moderation Voter
5 years ago

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?

2 answers

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

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.

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...

Ancestry

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. 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 to 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.

Not a mind reader

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 

The fix

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.


Hopefully this answered your question, and if it did, then don't forget to hit that "Accept Answer" button. If you have any other questions, then feel free to leave them down in the comments.
0
good answer starmaq 1290 — 5y
0
good answer starmaq 1290 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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)
0
Update your question with your new code Gey4Jesus69 2705 — 5y
0
it doesnt work it doesnt give me a error but it doesnt work hokyboy 270 — 5y
1
good answer starmaq 1290 — 5y
0
thank u Gey4Jesus69 2705 — 5y

Answer this question