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 = ...
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.
2 comments
good answer
good answer