Function being called at the incorrect moment

I’m working on a feature for my group homestore that lets you get infomation on a piece of clothing by right clicking it.

For this feature, I’m writing a new script for getting info on the pants, and to reduce the lines of code, I made a function that does the work and I simply connect it to a buy button. The function itself works fine, but it’s being called incorrectly when I playtest it, and if I fire the event it’s supposed to be called at, I get the message “attempt to call a nil value”.


This is what I get when I start playtesting in studio.

I’ve tried changing events and removing parameters, but they didn’t work.

Here’s the Code. It’s a localscript parented to StarterPlayerScripts.

-- Super Salty Peanuts
local Market = game:GetService("MarketplaceService")
local Info = game.Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui")
local InfoGui = Info.Frame
local BuyPants = game.Workspace.Bulding.Interior.BuyButtons.BuyPants



local function GetInfo (assetId, Date)
	local Pants = Market:GetProductInfo(assetId)
InfoGui.ItemName.Text = Pants.Name
InfoGui.Description.Description.Text = Pants.Description
InfoGui.Created.Text = "Created: "..Date
InfoGui.Sales.Text = "Sales: "..Pants.Sales
InfoGui.Price.Text = "Price: "..Pants.PriceInRobux
InfoGui.Visible = true
end

BuyPants.Misc.Rack1.BuyDotPants.ClickDetector.RightMouseClick:Connect(GetInfo(7183043338, "07/31/2021"))

You’re probably accidentally pressing it? It seems to be right click instead of left click. Idk if that was intentional or not.

Is the visibility off by default?

I assume what you mean by firing at the wrong time you mean right when you join the game.

Looking at the following line:
BuyPants.Misc.Rack1.BuyDotPants.ClickDetector.RightMouseClick:Connect(GetInfo(7183043338, "07/31/2021"))

It looks like you’re instructing to to call the function right away by calling the function incorrectly. Not sure how you’re getting the numbers you’re passing in GetInfo, but it may be better to do something like the following instead:

local Market = game:GetService("MarketplaceService")
local Info = game.Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui")
local InfoGui = Info.Frame
local BuyPants = game.Workspace.Bulding.Interior.BuyButtons.BuyPants



local function GetInfo(plr)
	local assetId = 7183043338
	local Date = "07/31/2021"
	local Pants = Market:GetProductInfo(assetId)
	InfoGui.ItemName.Text = Pants.Name
	InfoGui.Description.Description.Text = Pants.Description
	InfoGui.Created.Text = "Created: "..Date
	InfoGui.Sales.Text = "Sales: "..Pants.Sales
	InfoGui.Price.Text = "Price: "..Pants.PriceInRobux
	InfoGui.Visible = true
end

BuyPants.Misc.Rack1.BuyDotPants.ClickDetector.RightMouseClick:Connect(GetInfo)

Alternatively you could store the information in string values inside the part to get the information needed, and then reference it inside your function as something like BuyPants.Misc.Rack1.BuyDotPants.Date.Value or BuyPants.Misc.Rack1.BuyDotPants.assetId.Value