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.
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
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)
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.
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
...
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.
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.