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

Why is this throwing an "Unable to cast value to Object"? [Solved]

This script errors with "Unable to cast value to Object" I am not sure why as I'm pretty sure I'm including the player object. Let me know why.

Errors on Lines 51, 55, and 59 depending on which button you click.

local product1 = 616014127  

local product2 = 616014380

local product3 = 616014575  





--------------------Main stuff-----------------------------------------------------------
local datastore = game:GetService("DataStoreService")
local market = game:GetService("MarketplaceService")
local totalamount = datastore:GetDataStore("RobuxRaised")
local datakey = ("aafneueu7378348734bg")
local product1price = market:GetProductInfo(product1,Enum.InfoType.Product).PriceInRobux
local product2price = market:GetProductInfo(product2,Enum.InfoType.Product).PriceInRobux
local product3price = market:GetProductInfo(product3,Enum.InfoType.Product).PriceInRobux
local button1 = script.Parent.Donate1.SurfaceGui.TextButton
local button2 = script.Parent.Donate2.SurfaceGui.TextButton
local button3 = script.Parent.Donate3.SurfaceGui.TextButton
local raisedtext = script.Parent.TotalDonated.SurfaceGui.TextLabel
--[[
local loadamount = totalamount:GetAsync(datakey)

if not loadamount then
	pcall(function()
		totalamount:SetAsync(datakey,0)
	end)
else
	raisedtext.Text = "Total Donated: "..loadamount.."R$"
end

while true do
	raisedtext.Text = "Total Donated: "..loadamount.."R$"
	wait(3)
end

--]]
button1.Text = product1price.." R$"
button2.Text = product2price.." R$"
button3.Text = product3price.." R$"


button1.MouseButton1Down:Connect(function(plr)
	market:PromptProductPurchase(plr,product1)   --These error
end)

button2.MouseButton1Down:Connect(function(plr)
	market:PromptProductPurchase(plr,product2)  --These error
end)

button3.MouseButton1Down:Connect(function(plr)
	market:PromptProductPurchase(plr,product3)  --These error
end)

3 comments

0

If I'm not mistaken, it's market:PromptProductPurchase(product, plr) that's why. The player is an object and you need a value.

0

Nope, the player object argument comes before the asset argument.

0

Yeah. I was mistaken.

2 answers

You're using the first argument given by MouseButton1Down which is the x screen position for the mouse. See the other parameters here.

Since this is done locally you have access to the LocalPlayer from the Players service.

--Example usage for one product id.

local Players = game:GetService("Players")
local market = game:GetService("MarketplaceService")

local player = Players.LocalPlayer --This is what is needed

local button1 = script.Parent.Donate1.SurfaceGui.TextButton

local product1 = 616014127

local function PromptPurchaseOnClick()
	market:PromptPrudctPurchase(player, product1)
end

--Omitted position parameters here since we don't need them at all
button1.MouseButton1Down:Connect(PromptPurchaseOnClick)

I recommend trying to organize your variable names. Here are further articles to help you with Developer products:

#DevProducts

###Let me know if anything is wrong or if you had any other question.

1 comment

0

This is not a LocalScript. But I totally understand why anyone would think so due to it looking like a script for a GUI. It actually is, but it is for a surface GUI. Sorry I should have specified.

I have fixed this buy replacing the text buttons with click detectors. I appreciate you all trying to help.

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