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

Giving a player an item from a textbutton wont work?

I'm making a GUI where you click a button and if you own the shirt then it will give you an item, and if you don't own it, it will prompt a purchase.

local ASSET_ID = 123456 --Asset Id here
 
local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local PlayerOwnsAsset = MarketplaceService.PlayerOwnsAsset
 
script.Parent.MouseButton1Click:Connect(function(player)
	local success, doesPlayerOwnAsset = pcall(PlayerOwnsAsset, MarketplaceService, player, ASSET_ID)
	if doesPlayerOwnAsset then
		local clone = game.ReplicatedStorage.item:Clone()
		clone.Parent = player.Backpack
	else
		game:GetService("MarketplaceService"):PromptProductPurchase(player, ASSET_ID)
	end
end)

2 comments

0

What part of your script is the issue? Does it nit give the item? Does it not prompt the purchase?

0

It says "Attempt to index "Nil" with backpack"

1 answer

MouseButton1Click does not return a value so your player variable will always be nil.

In your LocalScript, you can get the current player by using

game:GetService("Players").LocalPlayer

If you need more info, you can find the documentation for MouseButton1Click here

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