How Do I Define Player?

Hi, I’m making this server script, but I’m not sure how to define player. I have a script similar to this but I don’t know how I defined player.

Old Script With Player Defined

game.ReplicatedStorage.CheckSale.OnServerInvoke = (function(player,item)--Responds to the remote function  request
local shopItems = game:GetService("ServerStorage"):WaitForChild("ShopItems")--Defines the items in ServerStorage
	
	local price = shopItems[item].Gold.Value--Defines the price of the item
	
	if player.leaderstats.Gold.Value >= price then--Check to see if the player has enough money
		
		player.leaderstats.Gold.Value = player.leaderstats.Gold.Value - price--Subtracts the price
		
		local gear = shopItems[item]:FindFirstChildOfClass("Tool"):Clone()--Clones the item they bought
		gear.Parent = player.Backpack--Put the clone in their backpack
		
		
		return true
	else
			return false
	
	end
end)

Script With Player Not Defined

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local upgrades = game.Players.LocalPlayer.PlayerScripts:WaitForChild('UpgradesIntValue')

local increase = 1.5
local price = upgrades.Value*increase

if player.leaderstats.Elixir.Value >= price then
	player.leaderstats.Elixir.Value = player.leaderstats.Elixir.Value - price
	
	upgrades.Value = upgrades.Value + 1 --- This is supposed to change the value of an IntValue am I doing this right?
	return true 
else
		return false
end
1 Like

On a local Script you can use

local player = game.Players.LocalPlayer

On a Server Script, we get players through:

local players = game.Players:GetPlayers()--Returns a table of all players in the game

or to get one player through a remote we can

Event.OnServerEvent:Connect(function(player)--player is the player who sent the remote event
--code
end)
--
17 Likes

Hi everyone, now that I just got ‘member’ trust so I can finally do something.

Anyway how can you get a specific player on a server script without using a function? I’d imagine that using:
local players = game.Players:GetChildren()
would be part of the answer, but I can’t seem to quite wrap my head around the concept.

5 Likes

game.Players:GetPlayers() for a table with all of the players in the server
game.Players.Username for a specific player in the server
game.Players:GetPlayerByUserId(UserId) for a player that may or may not be in the server

4 Likes

i think u can use this event

game.players.playeradded:connect(function(player)
       -- code
end)
2 Likes

This will fire once when the PlayerAdded event is fired upon the player entering the game, this does not additionally replicate the effects if Player.CharacterAdded is fired