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

Is there anything wrong with my code to purchase items?

I'm making a little GUI that prompts you to purchase an asset if the player does not own it, and simply changes the GUI to the booked screen if the player owns that asset, though I've used a few prints, I still can't find the error. Any help would be appreciated!

local btn = script.Parent
local class = script.Parent.Parent:WaitForChild("CLASS")
local ec = script.Parent.Parent:WaitForChild("EC")
local fc = script.Parent.Parent:WaitForChild("FC")
local slc = script.Parent.Parent:WaitForChild("SLC")
local title = script.Parent.Parent:WaitForChild("TITLE")
local MPC = game:GetService("MarketplaceService")


function CheckBCAndGive(plr)
	if MPC:PlayerOwnsAsset(plr,1253356769) then
		print 'Player owns asset'
		btn.Visible = false
		ec.Visible = false
		fc.Visible = false
		title.Text = "Thank you for flying with us!"
		slc.Text = "Class booked:"
		class.Text = "Business"
	else
		print 'Player does not own asset; prompting purchase'
		MPC:PromptPurchase(plr,1253356769)
	end
	
	
	
	
	
end

btn.MouseButton1Click:Connect(CheckBCAndGive)

The assets are T-shirts.

2 answers

✓ Accepted answer

The GuiButton.MouseButton1Click event has no parameters, so remove the plr parameter in line 10, and usePlayers.LocalPlayer since you used a LocalScript

Just for next time, if you use an event and it automatically has a player parameter:

Use a Server Script!




local btn = script.Parent
local class = script.Parent.Parent:WaitForChild("CLASS")
local ec = script.Parent.Parent:WaitForChild("EC")
local fc = script.Parent.Parent:WaitForChild("FC")
local slc = script.Parent.Parent:WaitForChild("SLC")
local title = script.Parent.Parent:WaitForChild("TITLE")
local MPC = game:GetService("MarketplaceService")


function CheckBCAndGive()
	if MPC:PlayerOwnsAsset(game.Players.LocalPlayer,1253356769) then
		print 'Player owns asset'
		btn.Visible = false
		ec.Visible = false
		fc.Visible = false
		title.Text = "Thank you for flying with us!"
		slc.Text = "Class booked:"
		class.Text = "Business"
	else
		print 'Player does not own asset; prompting purchase'
		MPC:PromptPurchase(game.Players.LocalPlayer,1253356769)
	end
	
	
	
	
	
end

btn.MouseButton1Click:Connect(CheckBCAndGive)

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