Receiving an item after purchase

how do i make it so the player gets the item right after they purchase the gamepass

for i,v in pairs (game.Workspace:GetDescendants()) do
	if v.name == "prompt1" then
		v.Touched:Connect(function(hit)
			local player1 = Players:GetPlayerFromCharacter(hit.Parent)
			if player then
			MarketplaceService:PromptGamePassPurchase(player1, gamepassID)
			end
		end)
	end
end
game:GetService('Players').PlayerAdded:Connect(function(player)
	player.CharacterAppearanceLoaded:Connect(function(character)
			if MarketPlaceService:UserOwnsGamePassAsync(player.UserId, gravID) then
			game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild("Backpack")
			game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild("StarterGear")

			end
		end)

end)
2 Likes

take a look at this PromptGamePassPurchaseFinished this allows you to check when something is bought like

MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(player , id , ispurchased))
if ispurchased then
 if id == "yourid" then
clone your item
end 
end
end)
3 Likes

thanks! this is what i was looking for

2 Likes
local players = game:GetService("Players")
local storage = game:GetService("ServerStorage")
local mps = game:GetService("MarketplaceService")
local gravCoil = storage.GravityCoil
local gravId = 0 --change to id of gamepass

players.PlayerAdded:Connect(function(player)
	if mps:UserOwnsGamePassAsync(player.UserId, gravId) then
		local starterGear = player.StarterGear
		gravCoil:Clone().Parent = starterGear
	end
	player.CharacterAppearanceLoaded:Connect(function(character)
		if mps:UserOwnsGamePassAsync(player.UserId, gravId) then
			local backpack = player.Backpack
			gravCoil:Clone().Parent = backpack
		end
	end)
end)

mps.PromptGamePassPurchaseFinished:Connect(function(player, gamepassId, wasPurchased)
	if wasPurchased then
		if gamepassId == gravId then
			local backpack = player.Backpack
			local starterGear = player.StarterGear
			gravCoil:Clone().Parent = backpack
			gravCoil:Clone().Parent = starterGear
		end
	end
end)
local players = game:GetService("Players")
local mps = game:GetService("MarketplaceService")
local gravId = 0 --change to id of gamepass

for i, v in ipairs(workspace:GetDescendants()) do
	if v.Name == "prompt1" then
		v.Touched:Connect(function(hit)
			local player = players:GetPlayerFromCharacter(hit.Parent)
			if player then
				mps:PromptGamePassPurchase(player, gravId)
			end
		end)
	end
end

Avoid cloning to the StarterGear folder inside a CharacterAdded/CharacterAppearanceLoaded event as this will fire each time the player’s character respawns/reloads thus resulting in the cloned tool (in this case gravity coil) appearing in the player’s inventory/backpack multiple times. In addition to this, when the same instance/service is referenced multiple times it’s best to explicitly declare an upvalue/nonlocal variable for it, and finally the first script you provided wouldn’t work.

local player1 = Players:GetPlayerFromCharacter(hit.Parent)
if player then
	MarketplaceService:PromptGamePassPurchase(player1, gamepassID)
end

The variable “player1” is declared and assigned a Boolean value but the conditional statement refers to a non-existent variable named “player” which points to nil always. Remember that “workspace” is the same as “game.Workspace”. On a final note, you were using the outdated property “name” instead of the current property “Name”, properties are case sensitive (name may work for now but it may seen become deprecated).

This is the final thing I’ll add, because “workspace:GetDescendants()” returns an array of instances it’s faster to iterate over its entries using the ipairs() iterator as opposed to pairs() iterator, but bare in mind that this will only work for arrays, for dictionaries pairs() must be used.

2 Likes