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!