Hello! Recently, I've been trying to fix one of my scripts.
This script is linked to a Dev Product. When the product is bought, it is intended to give everyone in the server + 50 Tokens (including the player who bought it!).
But... when the player purchases the product, nothing happens!
I tested the script out originally so that only the player who bought it received the + 50 Tokens, which worked. But the script which is supposed to give every player in the server the + 50 Tokens didn't... (FYI, both scripts were placed in ServerScriptService)
This is the script which only gives one player the + 50 Tokens. (Script1)
local MarketplaceService = game:GetService('MarketplaceService')
local devproductId = 389417210 -- The ID of the dev product.
MarketplaceService.ProcessReceipt = function(receiptInfo)
for i, player in ipairs(game.Players:GetChildren()) do
if player.userId == receiptInfo.PlayerId then
if receiptInfo.ProductId == devproductId then
-- Receive tokens
player.leaderstats.Tokens.Value = player.leaderstats.Tokens.Value + 50
end
end
end
return Enum.ProductPurchasedDescision.PurchaseGranted
end
After finding that the other script (script above) worked, I tried this script, which is the script that is supposed to give everyone in the server + 50 Tokens. (Script2)
local MarketplaceService = game:GetService('MarketplaceService')
local devproductId = 389417210 -- The ID of the dev product.
local players = game:GetService("Players")
MarketplaceService.ProcessReceipt = function(receiptInfo)
for i, player in ipairs(game.Players:GetChildren()) do
if player.userId == receiptInfo.PlayerId then
if receiptInfo.ProductId == devproductId then
-- Receive Tokens
players.leaderstats.Tokens.Value = players.leaderstats.Tokens.Value + 50
end
end
end
return Enum.ProductPurchasedDescision.PurchaseGranted
end
I'm not sure if I'm missing something, or if something is wrong. I'm relatively new to scripting, so Script2 is probably completely wrong.
If anyone could help me out a bit, that would be great!

4 comments
Players is not a actual "player" its a Service
Use game:GetService("Players"):GetPlayers() get service is the best way to get a service and getplayers is the recommended way to get players
userId is deprecated, use UserId. You know that GetPlayerByUserId exists right?
Simply remove the check you're making: if player.userId == receiptInfo.PlayerId then