How to get the player from a server script? [Solved!]

So basically I am trying to get the player inside a server script so I can access their datastore and change values. I have heard about Remote Events but that’s only for getting data from local scripts to server scripts right? The datastore has been made in a module.

This is what I currently have but I need to define the local player to the player but I don’t know how

local tab = require(game.ServerScriptService.PlayerData)
local player = 
local playerStats = tab.GiveData(player)
local kills = playerStats["Kills"]
local Humanoid = script.Parent.Humanoid
print(script.Parent.Parent.Name)

ANSWER

Okay, so there are multiple ways to get access to the players information on the server side.

One of them is to simply add this line of code into the server sided script and it will get the players data as soon as he joins the game.

player = game:GetService("Players").PlayerAdded:Wait()

This can also be done by putting your code inside a PlayerAdded event.

game.Players.PlayerAdded:Connect(function(plr)
    -- code here
end)

If the script is a child of the characters model, we can also reference the player with the following line of code:

local player = game.Players:GetPlayerFromCharacter(script.Parent)

The last way is to use remote events:

Local script
game.ReplicatedStorage.YourRemoteEvent:FireServer()

Server script

game.ReplicatedStorage.YourRemoteEvent.OnServerEvent:Connect(function(Player)

end)

Hope this helps!

48 Likes

You can actually use FireClient() instead of FireServer() and that goes from a local script to a server script. But you put the FireClient in a server script, and do OnClientEvent in a local script, just an fyi

6 Likes

I have no knowledge with scripting but maybe do this.

local plr = game.Players.LocalPlayer

Example

script.parent.parent.Mousebutton1click:connect(function(plr)

5 Likes

You can only use LocalPlayer in a local script; OP is using a server script.

Since he’s referencing the player’s humanoid with script.Parent.Humanoid we can assume that this script is a child of the character model, so they can reference the player with:

local player = game.Players:GetPlayerFromCharacter(script.Parent)
25 Likes

You can put your code inside a PlayerAdded event.

game.Players.PlayerAdded:Connect(function(plr)
    -- code here
end)
25 Likes

Okay but the datastore has been made in a module will it still work then?

3 Likes

No sorry that won’t work as the player isn’t clicking on anything but thanks for your help :slight_smile:

4 Likes

As @changeno said, you’ll need to use a PlayerAdded event for this, not remote events.

4 Likes

he could get it from a remote event by firing a remote on the client and doing this on the server

yourremote.OnServerEvent:Connect(function(plr)
    local player = plr
    print(player.Name)
end

although i am not sure if it is very efficient i use it for moves/skills

6 Likes

That’s was just an example. Use this variable inside you function

3 Likes

There’s no need for remote events, only time you should get data store information for this situation is on a PlayerAdded event. Then you’ll store those values into objects, intvals, stringvals, boolvals etc. then you can always update the datastore using those object values.

4 Likes

he was asking how to get the player from a serverscript, my code is a valid solution its just not going to be good for his situation, my code answers the title

3 Likes

PlayerAdded Events takes an argument of Player. Being the player who was added.

game:GetService("Players").PlayerAdded:Connect(function(Player)
    --<The server now has full control over that player object.
--[[
Here is where you would load the players data
store them into values so it can't be accessed anytime and anywhere by
any other scripts. This doesn't answer the question?
]]
end)

I really can’t see how RemoteEvents would be necessary in this situation.

6 Likes

3 Ways

  • First way Player added event.
  • Making a loop function for all players in game.
  • Remotes Events (Not recommended)

First way Code:

game.Players.PlayerAdded:Connect(function(plr)

end)

Second Way Code:

for _,Player in pairs(game.Players:GetPlayers()) do
	
end

Third Way Code

Local Script

game.ReplicatedStorage.YourRemoteEvent:FireServer()

Server Script

game.ReplicatedStorage.YourRemoteEvent.OnServerEvent:Connect(function(Player)

end)
5 Likes

my personal favorite way of getting the player is just by doing:

local PLR = game.Players.PlayerAdded:Wait()

local CHR = PLR.CharacterAdded:Wait()

see if that helps.

5 Likes
game.Players.PlayerAdded:Connect(function(player) -- the player
   local tab = require(game.ServerScriptService.PlayerData)
   local playerStats = tab.GiveData(player)
   local kills = playerStats["Kills"]
   local Humanoid = script.Parent.Humanoid
   print(script.Parent.Parent.Name)
end)
2 Likes

Whole script would be useful please.

2 Likes

this post is from 2020

1 Like

Hmmmmm… lol it was active so i decided to reply wothout checking the date lol

1 Like

You cant use localplayer in a server script