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

How can I be able to make this script award 50 points?

How can I make this script be able to award a player 50 points after the player has bought this??

local player = script.Parent.Parent.Parent.Parent.Parent.Parent
print(player)
 local MarketplaceService = Game:GetService("MarketplaceService")
local buyButton = script.Parent
local productId = 20792308
 
buyButton.MouseButton1Click:connect(function()
	MarketplaceService:PromptProductPurchase(player, productId)
end)

local MarketplaceService = Game:GetService("MarketplaceService")
 
MarketplaceService.ProcessReceipt = function(receiptInfo)
	print(receiptInfo.PlayerId .. " just bought " .. receiptInfo.ProductId)
	wait(1)
	player:FindFirstChild("leaderstats").Points = player:FindFirstChild("leaderstats").Points + 50
	-- ...
	-- use DataStore to record purchase
	-- ...	
	return Enum.ProductPurchaseDecision.PurchaseGranted		
end

2 comments

0

Using FindFirstChild without error checking is stupid -- it misleads the reader into thinking you do error check, yet will your code will error anyway. .leaderstats suffices.

0

Ok, so how do I fix it then. It still errors without it.

3 answers

13:17:41 -- Players.billybobtijoseph.PlayerGui.Shop.Page1.BuyPointsFrame.Button1: 16: attempt to perform arithmetic on field 'Points' (a userdata value)

Replace the line:

 player:FindFirstChild("leaderstats").Points = player:FindFirstChild("leaderstats").Points + 50

with:

 player:FindFirstChild("leaderstats").Points.Value = player:FindFirstChild("leaderstats").Points.Value + 50

You're using value holder objects. You set their values through their Value property.

What you're actually asking it to add is userdata not a number. The solution is to add .Value towards the end since you cannot perform arithmetic on this.

local player = script.Parent.Parent.Parent.Parent.Parent.Parent
print(player)
 local MarketplaceService = Game:GetService("MarketplaceService")
local buyButton = script.Parent
local productId = 20792308

buyButton.MouseButton1Click:connect(function()
    MarketplaceService:PromptProductPurchase(player, productId)
end)

local MarketplaceService = Game:GetService("MarketplaceService")

MarketplaceService.ProcessReceipt = function(receiptInfo)
    print(receiptInfo.PlayerId .. " just bought " .. receiptInfo.ProductId)
    wait(1)
	--Fixed it here
    player:FindFirstChild("leaderstats").Points.Value = player:FindFirstChild("leaderstats").Points.Value + 50
    -- ...
    -- use DataStore to record purchase
    -- ...  
    return Enum.ProductPurchaseDecision.PurchaseGranted     
end

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