How can i get the Local Player from this script?

  1. What do you want to achieve? Hello, in a serverscript related to an in-game purchase, I would like to be able to detect the player in order to perform an action on him, but I do not know how to do it. Thanks
    I have tried :
for i, plr in pairs(game.Players:GetPlayers()) do --But it take all player in the game
--//// i also tried
local Players = game:GetService('Players').PlayerAdded:Wait() -- But it only take the last player

Here is the function where i want to take the player :

mps.ProcessReceipt = function(Shop)
	--Here i want to detect the player to put an action for him
	local amount = mps:GetProductInfo(Shop.ProductId, Enum.InfoType.Product).PriceInRobux

	local success, err = pcall(function()
		local totalDonated = ods:GetAsync(Shop.PlayerId) or 0
		ods:SetAsync(Shop.PlayerId, totalDonated + amount)
	end)
	return success and Enum.ProductPurchaseDecision.PurchaseGranted or Enum.ProductPurchaseDecision.NotProcessedYet
end
1 Like

Where is this script located?

[dont mind this]

Server script in ServerScriptService

[dont mind this]

1 Like

There is no way to define the player thanks to this function ?

mps.ProcessReceipt = function(Shop)

There is no ‘local player’ because you’re in a serverside setting. If you want to get a specifc player, you do:

-- e.g.

game:GetService("Players"):FindFirstChild("Downrest") -- replace 'Downrest' with a name
1 Like

Yes but in a server script it is sometimes possible to have only one player, especially with Events or with game:GetService(‘Players’) Players.PlayerAdded:Connect(function(plr) .

Because I can’t put a name on the player who is going to buy my product since I don’t know him, he is one of hundreds of millions of players I can’t predict. There must be a way, I hope

Hello!
I’m pretty sure you can’t get LocalPlayer on Server scripts since when you try it, it will return nil.
I suggest try using the code block listed below on a LocalScript and use an event to link the function to a ServerScript.

local Players = game:GetService(“Players”).LocalPlayer

If you really want to learn more, check out the Developer Hub.

1 Like

This is impossible since the mps.ProcessReceipt = function(Shop) is only accessible in a server script. So the use of an event is not possible in this case.

Same thing for the DataStore (there is one in the script)

mps.ProcessReceipt = function(Shop)

Shop will also contain the PlayerID of the player who purchased the product so you can use that:

local Plr=Players:GetPlayerByUserId(Shop.PlayerId)
3 Likes

Please read the documentation, you can find the player by accessing ReceiptInfo.PlayerId and looping through players table and see if that player’s UserId is the same as the one on ReceiptInfo.

2 Likes
local player = game:GetService("Players"):GetPlayerByUserId(Shop.PlayerId)

If this is a gamepass, I am pretty sure you can just do:

mps:PromptGamePassPurchaseFinished:Connect(function(plr, id, purchased) — First value is for the player, second is for the gamepass id, and third, to find out if it was purchased.
    — Code here
end)

Hope this helps :slight_smile:

local Player = Players:GetPlayerByUserId(Shop.PlayerId)
if Player then

Where ‘Players’ is a reference to the service of the same name.
https://developer.roblox.com/en-us/api-reference/callback/MarketplaceService/ProcessReceipt

1 Like