Getting the button instance when it is clicked

I am trying to make a shop Gui. (Screenshot of what it looks like below) When I button is clicked I am trying to get the player and which button instance is returned, but I don’t know and cant find the parameters you can give from the MouseButton1Click function to the checkPurchase function.

image

local sP = script.Parent

local function checkPurchase(button)
print(button.Parent.ItemID … “button pressed”)
end

for i, button in ipairs (sP.Store:GetDescendants()) do

if button.ClassName == 'TextButton' and button.Name == "BuyButton" then

	button.MouseButton1Click:Connect(checkPurchase)
	
end

end

This is the error I get:
Players.Its4Realzies.PlayerGui.StoreGui.StoreGuiScript:40: Expected ‘)’ (to close ‘(’ at line 38), got ‘end’

local sP = script.Parent

local function checkPurchase(button)
   print(button.Parent.ItemID .. “button pressed”)
end

for i, button in ipairs(sP.Store:GetDescendants()) do
   if button.ClassName == 'TextButton' and button.Name == "BuyButton" then
	button.MouseButton1Click:Connect(checkPurchase)
   end
end)

You forgot to close of the ipairs by doing end) instead of end

Alternatively you could do this:

for _, frame in pairs(sP.Store:GetChildren()) do
   if not frame:IsA("Frame") then continue end
   frame.BuyButton.MouseButton1Down:Connect(function()
      print(frame.ItemID.Value.." Pressed")
   end)
end)

No, that is not the case, that actually creates an error. ipairs is already closed and do threads don’t require a ).

Can you provide a bigger example of the code please?

Oh right, my bad I just realised that. I think I just assumed there would be ) because of the error. We need more code since I don’t think the error is on the code @Its4Realzies has shown.

1 Like

I posted the entirety of the code that is relevant to this

I can assure you it is >

https://streamable.com/9uivg6Thankyou I realized I posted the wrong error. Here is a video and the right one >

It’s because you’re giving a function that requires one parameter (and uses it), but you actually aren’t feeding that parameter to it. Try this:

...
if button.ClassName == 'TextButton' and button.Name == "BuyButton" then

	button.MouseButton1Click:Connect(function() -- Give it an anonymous function
        checkPurchase(button) -- Fire the function with the button parameter
    end)
	
end
...
1 Like

Thankyou it works, but I dont understand why. What makes this one get the parameter that the other one does not?

1 Like

I see your function checkPurchase requires an argument that you aren’t specifying. The argument is a button and you aren’t giving it the button that it needs therefore it errors so what you need to do is what zamd157 said.

1 Like

What parameters can the touched button function give? The only ones I see are left, right and up

Think of it as this:
When the .MouseButton1Click event fires, internally Roblox fires the function you’ve provided. Now, does .MouseButton1Click give any arguments? Nope! That’s why it was erroring.

-- Somewhere in Roblox's code

-- Click handler
obj:FireEvent()

function obj:FireEvent()
    -- Send the signal for the developer's code to be executed
    self._function() -- Function fired by the developer
end

See how it doesn’t give the button argument? For example, the .PlayerAdded event might look like this:

function obj:FireEvent(player)
    -- Fire the signal for the developer's code to be executed
    self._function(player)
end

Where self._function is the function provided when connecting to the event.
That’s why we’re creating a function, to fire the function with the button argument provided by the ipairs loop.

1 Like

Thankyou that makes sense now! :grin:

1 Like