Clicking children in a frame with a loop makes the buttons be stored and click multiple times?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I’m trying to call a function everytime a child of a frame is clicked.

  2. What is the issue? Include screenshots / videos if possible!
    It works for the first button, but when I get to the second button it calls the function twice, the first time it’s called it is called with the name of the button that just clicked, but the second time it is called it is called with the name of the button that I first clicked.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Tried searching up solutions, couldn’t find anything of the same issue.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local plr = game.Players.LocalPlayer

local function devProduct(buttonname)
	game.ReplicatedStorage.devProduct:FireServer(buttonname)
end

plr.PlayerGui:WaitForChild("ScreenGui")

for i, button in pairs(plr.PlayerGui.ScreenGui.Frames.TrollMenu.Background:GetChildren()) do
	if button:IsA("ImageButton") and button.Name ~= "Spectate" and button.Name ~= "SpecRight" and button.Name ~= "SpecLeft" then
		button.MouseButton1Click:Connect(function()
			devProduct(button.Name)
		end)
	end
end
8 Likes

Could you give an output/error log? This has happened to me before and it’s usually because there are duplicate buttons or some error in the code.

2 Likes

Where is this script located? If it’s located as a descendant of a ScreenGui and .ResetOnSpawn is set to true, then this script will rerun whenever you reset.

2 Likes

Sure thing.

  12:39:44.993  killed  -  Server - Settings:49
  12:39:54.025  tripped  -  Server - Settings:60
  12:39:54.026  killed  -  Server - Settings:49

That’s all the output, the first line is where I clicked the KILL button, the second and third line is where I clicked just the TRIP button.

2 Likes

It is not a descendant of a ScreenGui, it is in ServerScriptService.

2 Likes

That doesn’t work in a Script located in ServerScriptService. Just an FYI.

That may be the problem, however, it doesn’t seem like it is. Make sure that the ScreenGui has it’s ResetOnSpawn property disabled, just for testing right now.

Let me know what happens.

1 Like

I apologize, it is a LocalScript in StarterPlayerScripts, got confused for a second. ResetOnSpawn is already disabled.

2 Likes

It’s definitely an issue with the for loop then. The only thing that I can think of is that the for loop is getting reran every time the second button is pressed. Is there anything that would cause this?

1 Like
local Settings = require(game.ServerStorage.Settings)

local function promptDevProduct(plr, buttonname)
	local userId = game:GetService("Players"):GetUserIdFromNameAsync(plr)
	local button = Settings.TrollMenu[buttonname]["DevProductID"]
	game:GetService("MarketplaceService"):PromptProductPurchase(plr, button)
	game:GetService("MarketplaceService").PromptProductPurchaseFinished:Connect(function(userId,button,isPurchased)
		if isPurchased then
			Settings.TrollMenu[buttonname.. "Player"](plr)
		end
	end)
end

game.ReplicatedStorage.devProduct.OnServerEvent:Connect(function(plr, buttonname) promptDevProduct(plr, buttonname) end)

Is the script in ServerScriptService that the remote event is being fired to, it actually isn’t an issue with the for loop I don’t believe, whenever I print the button that is being clicked, it prints normally.
Example
-presses Kill-
-doesnt buy-
-presses Freeze-
-buys-
Freeze and Kill are both activated, even though Kill wasn’t bought.

2 Likes

The plr argument for :GetUserIdFromNameAsync() should be a player name value, not an instance. That could be part of your issue?

1 Like

Fixed that, but it did not resolve the issue.

1 Like

Have you thought of anything else?

1 Like

No. I can’t think of anything right now. I will let you know if I do. Sorry!

1 Like

Fixed this, had to disconnect the event.

1 Like

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