Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

attempt to concatenate string with nil. how to fix this?

Asked by 3 years ago
local MarketplaceService = game:GetService("MarketplaceService")
local plr = game.Players.LocalPlayer

local ID = 111111--11925441
local BuyBtn = script.Parent.BuyBtn
local Cost = script.Parent.Cost
local ProductInfo = MarketplaceService:GetProductInfo(ID, Enum.InfoType.Product)
local Price = ProductInfo.PriceInRobux

BuyBtn.MouseButton1Click:Connect(function()
    MarketplaceService:PromptGamePassPurchase(plr, ID)
end)

if MarketplaceService:UserOwnsGamePassAsync(plr.UserId,ID) then
    BuyBtn.Visible = false
    Cost.Text = "Owned"
else
    BuyBtn.Visible = true
    Cost.Text = "R$"..Price
end

1 answer

Log in to vote
0
Answered by
imKirda 4491 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

Well first, PriceInRobux is nil in your case so doing

Cost.Text = "R$"..Price

is same as doing

Cost.Text = "R$"..nil

It can't work, to get PriceInRobux without it being nil you need to change line 7 from Enum.InfoType.Product to Enum.InfoType.GamePass so

local ProductInfo = MarketplaceService:GetProductInfo(ID, Enum.InfoType.GamePass)

Now it will get your PriceInRobux, but it will still error that you are trying to compensate number with string because

Cost.Text = "R$"..Price

is same as

Cost.Text = "R$"..300 -- for example

Which you also can't do, to fix this you can use 2 ways. First way is tostring:

Cost.Text = "R$"..tostring(PriceInRobux)

Which will convert given value into string. The second way is using string.format:

Cost.Text = string.format("R$ %d", PriceInRobux)

About string patterns here, Basically %d is pattern for numbers so it takes second argument which is PriceInRobux and formats it into the %d pattern so the result should be R$ 300 (Not 300 but PriceInRobux). This might not work as i have no idea what i am doing i haven't used MarketplaceService since 2019. If it did not help, don't accept it as an answer.

Ad

Answer this question