Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

i keep getting an error saying that player isnt part of players, why?

this is in a script in serverscripservice

heres my code

local Players = game:GetService("Players")
local player = game.Players.PlayerAdded
local productID = 1071179589
local MarketplaceService = game:GetService("MarketplaceService")
local productInfo = MarketplaceService:GetProductInfo(productID, Enum.InfoType.Product)
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local function processReceipt(receiptInfo)
	
	print("remote event working")
	local player = game:GetService("Players"):GetPlayerByUserId(receiptInfo.PlayerId)
	if not player then
		-- player probably left
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	if player then
		print ("if player then working")
		game.Players.player.leaderstats.lives.Value = game.Players.player.leaderstats.lives.Value + 1
		return true
	end
	return Enum.ProductPurchaseDecision.PurchaseGranted
end

	MarketplaceService.ProcessReceipt = processReceipt

1 answer

game.Players.PlayerAdded is an event. You can't define events as variables. Instead, you might want to create a function just under the variables. Which would change a global variable. This is what I mean:


local Players = game:GetService("Players")
local player --We are setting this variable to nil for now.
local productID = 1071179589
local MarketplaceService = game:GetService("MarketplaceService")
local productInfo = MarketplaceService:GetProductInfo(productID, Enum.InfoType.Product)
local ReplicatedStorage = game:GetService("ReplicatedStorage")

Players.PlayerAdded:Connect(function(newPlayer)
	player = newPlayer
end)

local function processReceipt(receiptInfo)

    print("remote event working")
    local player = game:GetService("Players"):GetPlayerByUserId(receiptInfo.PlayerId)
    if not player then
        -- player probably left
        return Enum.ProductPurchaseDecision.NotProcessedYet
    end
    if player then
        print ("if player then working")
        game.Players.player.leaderstats.lives.Value = game.Players.player.leaderstats.lives.Value + 1
        return true
    end
    return Enum.ProductPurchaseDecision.PurchaseGranted
end

    MarketplaceService.ProcessReceipt = processReceipt

I hope this helps!

This discussion is preserved as it was. New answers, comments, and votes are closed.