Function firing for no reason

I have an inventory script that makes a UI pop up on the side when you select an item.
But, for some reason, the function fires instantly and does not fire when you click the button.
It also gives an error (image at the bottom)

Script in the button:

local function loadGUI(item)
	print("e")
	local plate = game:GetService("Players").LocalPlayer.Values.Items.Building.SmallPlate
	local player = game:GetService("Players").LocalPlayer
	local values = player:WaitForChild("Values")
	local selected = values:WaitForChild("SelectedItem")
	local ID = item.ImageID.Value
	
	script.Parent.Parent.Parent.Parent.Specs.Title.Text = "Small Plate"
	script.Parent.Parent.Parent.Parent.Specs.Owned.Text = tostring(item.Value)
	script.Parent.Parent.Parent.Parent.Specs.Image.Image = ID
	script.Parent.Parent.Parent.Parent.Specs.Visible = true
end

local plate = game:GetService("Players").LocalPlayer.Values.Items.Building.SmallPlate

script.Parent.MouseButton1Down:Connect(loadGUI(plate))

image

Screenshot 2022-05-18 105202

You can’t connect a function and pass variables that way. The function automatically takes the variables that are provided by the event. To change this, use an built in function to trigger another function. Like so:

script.Parent.MouseButton1Down:Connect(function()
   loadGUI(plate)
end)

This would work.

1 Like