Does the logic of this code made sense, or won't it work?

I’ve never used pcall so idk if this makes any sense, someone told me I could use it if I wasn’t sure how many buttons there would be (any number less than 21). Please let me know if theres some issues with this code.

pcall(function ()
						local button1 = loser.PlayerGui.Donation.Frame.itemsScroller.DonateButton1
					end)
					pcall(function ()
						local button2 = loser.PlayerGui.Donation.Frame.itemsScroller.DonateButton2
					end)
					pcall(function ()
						local button3 = loser.PlayerGui.Donation.Frame.itemsScroller.DonateButton3
					end)
					pcall(function ()
						local button4 = loser.PlayerGui.Donation.Frame.itemsScroller.DonateButton4
					end)
					pcall(function ()
						local button5 = loser.PlayerGui.Donation.Frame.itemsScroller.DonateButton5
					end)
					pcall(function ()
						local button6 = loser.PlayerGui.Donation.Frame.itemsScroller.DonateButton6
					end)
					pcall(function ()
						local button7 = loser.PlayerGui.Donation.Frame.itemsScroller.DonateButton7
					end)
					pcall(function ()
						local button8 = loser.PlayerGui.Donation.Frame.itemsScroller.DonateButton8
					end)
					pcall(function ()
						local button9 = loser.PlayerGui.Donation.Frame.itemsScroller.DonateButton9
					end)
					pcall(function ()
						local button10 = loser.PlayerGui.Donation.Frame.itemsScroller.DonateButton10
					end)
					pcall(function ()
						local button11 = loser.PlayerGui.Donation.Frame.itemsScroller.DonateButton11
					end)
					pcall(function ()
						local button12 = loser.PlayerGui.Donation.Frame.itemsScroller.DonateButton12
					end)
					pcall(function ()
						local button13 = loser.PlayerGui.Donation.Frame.itemsScroller.DonateButton13
					end)
					pcall(function ()
						local button14 = loser.PlayerGui.Donation.Frame.itemsScroller.DonateButton14
					end)
					pcall(function ()
						local button15 = loser.PlayerGui.Donation.Frame.itemsScroller.DonateButton15
					end)
					pcall(function ()
						local button16 = loser.PlayerGui.Donation.Frame.itemsScroller.DonateButton16
					end)
					pcall(function ()
						local button17 = loser.PlayerGui.Donation.Frame.itemsScroller.DonateButton17
					end)
					pcall(function ()
						local button18 = loser.PlayerGui.Donation.Frame.itemsScroller.DonateButton18
					end)
					pcall(function ()
						local button19 = loser.PlayerGui.Donation.Frame.itemsScroller.DonateButton19
					end)
					pcall(function ()
						local button20 = loser.PlayerGui.Donation.Frame.itemsScroller.DonateButton20
					end)	
							
					local function donateButtonClicked(donateButton)
			
						local itemId = donateButton.Value.Value
						local itemPrice = donateButton.Price.Value

						MarketPlaceService:PromptPurchase(loser, itemId)

						MarketPlaceService.PromptPurchaseFinished:Connect(function(loser, itemId, was_purchased)

							if was_purchased then
								
								loser.leaderstats.Donated.Value += itemPrice
								local playerId = loser.UserId
								
								game.StarterGui:SetCore("ChatMakeSystemMessage", {
									Text = "[SYSTEM] "..loser.." donated "..itemPrice.." robux to "..winnerName;
									Font = Enum.Font.Cartoon;
									Color = Color3.new(0,255,0);
									FontSize = Enum.FontSize.Size96;
								})
							end

						end)
					end
					
					button1.MouseButton1Click:Connect(function()
						donateButtonClicked(button1)
					end)
					button2.MouseButton1Click:Connect(function()
						donateButtonClicked(button2)
					end)
					button3.MouseButton1Click:Connect(function()
						donateButtonClicked(button3)
					end)
					button4.MouseButton1Click:Connect(function()
						donateButtonClicked(button4)
					end)
					button5.MouseButton1Click:Connect(function()
						donateButtonClicked(button5)
					end)
					button6.MouseButton1Click:Connect(function()
						donateButtonClicked(button6)
					end)
					button7.MouseButton1Click:Connect(function()
						donateButtonClicked(button7)
					end)
					button8.MouseButton1Click:Connect(function()
						donateButtonClicked(button8)
					end)
					button9.MouseButton1Click:Connect(function()
						donateButtonClicked(button9)
					end)
					button10.MouseButton1Click:Connect(function()
						donateButtonClicked(button10)
					end)
					button11.MouseButton1Click:Connect(function()
						donateButtonClicked(button11)
					end)
					button12.MouseButton1Click:Connect(function()
						donateButtonClicked(button12)
					end)
					button13.MouseButton1Click:Connect(function()
						donateButtonClicked(button13)
					end)
					button14.MouseButton1Click:Connect(function()
						donateButtonClicked(button14)
					end)
					button15.MouseButton1Click:Connect(function()
						donateButtonClicked(button15)
					end)
					button16.MouseButton1Click:Connect(function()
						donateButtonClicked(button16)
					end)
					button17.MouseButton1Click:Connect(function()
						donateButtonClicked(button17)
					end)
					button18.MouseButton1Click:Connect(function()
						donateButtonClicked(button18)
					end)
					button19.MouseButton1Click:Connect(function()
						donateButtonClicked(button19)
					end)
					button20.MouseButton1Click:Connect(function()
						donateButtonClicked(button20)
					end)
2 Likes

No. This is absolutely wrong man.

3 Likes

Ok so, how would I find out if a button has been clicked, and the name of the button that has been clicked?

2 Likes
  1. You’re defining the button variables inside of the pcall function, so they will only exist inside that function. When you’re trying to use those variables later in the script, it will error since that part of the script will not know they exist, if that makes sense.
  2. pcall should be used to catch an error that you can not prevent. As an example, some services like DataStoreService will occasionally error, and you want to catch those errors.
local success, result = pcall(function()
    return DataStoreService:GetAsync(...)
end)
if success then
    ...
else
   warn("Failed to get data, retrying")
   ...
end

In this case, you probably want the script to error if the buttons do not exist. Try using WaitForChild to wait for the buttons to load in.

2 Likes

Thanks for your help, but basically in my script I get the players t shirts for sale and put them on buttons, so basically the amount of buttons changes every time, so it will just be waiting forever for the buttons that arent actually going to be added, wont it?

1 Like

pcalls are used to catch errors and enable the code to continue running.
Alternatively since this will not error, I shortened it by a lot here (if there are only buttons inside the frame):

							
					local function donateButtonClicked(donateButton)
			
						local itemId = donateButton.Value.Value
						local itemPrice = donateButton.Price.Value

						MarketPlaceService:PromptPurchase(loser, itemId)

						MarketPlaceService.PromptPurchaseFinished:Connect(function(loser, itemId, was_purchased)

							if was_purchased then
								
								loser.leaderstats.Donated.Value += itemPrice
								local playerId = loser.UserId
								
								game.StarterGui:SetCore("ChatMakeSystemMessage", {
									Text = "[SYSTEM] "..loser.." donated "..itemPrice.." robux to "..winnerName;
									Font = Enum.Font.Cartoon;
									Color = Color3.new(0,255,0);
									FontSize = Enum.FontSize.Size96;
								})
							end

						end)
					end
					
for i,v in pairs(loser.PlayerGui.Donation.Frame.itemsScroller:GetChildren()) do
if not (v:IsA("TextButton") or v:IsA("ImageButton")) then continue end
v.MouseButton1Click:Connect(function()
donateButtonClicked(v)
end)
end
1 Like

Just copy everything, except the pcall(function()…

Thanks for your help, on this line the ‘then’ has red line underneath it.

if not (v:IsA("TextButton") or v:IsA("ImageButton") then continue end

Then loop until you reach the number of T-Shirts they have for sale, like this:

for i = 1, numOfTShirtsForSale do
	local button = loser.PlayerGui.Donation.Frame.itemsScroller:WaitForChild("DonateButton" .. i) -- Waits for the button to load in if it does not already exist, preventing an error
	button.Activated:Connect(function() -- Runs when the button is activated
		donateButtonClicked(button)
	end)
end
1 Like

I made a typo and fixed that in the code already, I forgot to add a parenthese

1 Like

Thanks, its all working great, the only thing is for some reason my system chat message doesn’t appear, any ideas why?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.