Why does this event fire multiple times and how it can be fixed?

Hi Developers! :wave:

So I have a local script which creates buttons, and when you press them it fires a ServerEvent. It needs to fire one time, but for some reason it fires 7 TIMES

Here’s the code:

Client
newTypeButton.MouseButton1Click:Connect(function()
				
				
				for i, child in pairs(loadout.WeaponsList:GetChildren()) do

					if child:IsA("TextButton") then child:Destroy() end
				end
				
				for i, weaponChild in ipairs(weaponType:GetChildren()) do

					local newWeaponButton = script.WeaponButton:Clone()
					newWeaponButton.Text = weaponChild.Name
					newWeaponButton.Name = weaponChild.Name
					newWeaponButton.Parent = loadout.WeaponsList
					
					print(weaponChild, weaponChild.ClassName)
					
					
					local statgui = frame.Parent.StatsFrame
					
					local UnlockedStatus = getunlockedEvent:InvokeServer(weaponChild.Name)
					if UnlockedStatus == nil then
						UnlockedStatus = false
					end
					print(UnlockedStatus, weaponChild.Name)

					newWeaponButton.MouseButton1Click:Connect(function()
						
						local SettingModule = require(weaponChild:WaitForChild("Setting"))
						local StatsModule = require(weaponChild:WaitForChild("Setting"):WaitForChild("1"))
						
						local Price = getpriceEvent:InvokeServer(weaponChild.Name)
						
						print(Price, "price for", weaponChild.Name, UnlockedStatus)
						
					end)
					
					if UnlockedStatus then
						loadout.WeaponsList[weaponChild.Name].LockedUI.Visible = false
					end
					
					--update UI if weapon has been bought
					
					rs.WeaponSystemEvents.UpdateWeapon.OnClientEvent:Connect(function()
						print("updated UI")
					end)
					
					
					--==================buy and equip events fire=====================   HERE IS THE PROBLEM

					local equipEvent = rs.WeaponSystemEvents.Equip --event with the problem

					loadout.BuyDescFrame.EquipButton.MouseButton1Click:Connect(function() equipEvent:FireServer(weaponChild.Name) updateLoadout() end) --firing this event
					
					
				end
			end)
Server
------prices------
	local weaponPrices = {

		--historical
		["G36"] = {
			name = "G36",
			price = 0,
			class = "Primary",
		},
		["Five-Seven"] = {
			name = "Five-Seven",
			price = 0,
			class = "Secondary",
		},
		["Glock 17"] = {
			name = "Glock 17",
			price = 9000,
			class = "Secondary",
		},
		["SKS"] = {
			name = "SKS",
			price = 3300,
			class = "Primary",
		},
		["Remington 870"] = {
			name = "Remington 870",
			price = 7200,
			class = "Primary",
		},

		--unnatural
		["Fire"] = {
			name = "Fire",
			price = 1800,
			class = "Secondary",
		},
		["Magic Stick"] = {
			name = "Magic Stick",
			price = 3000,
			class = "Primary",
		},

	}
	------------------


--[[weaponTable is the data, here is the default structure

local weaponTable = {
		OwnedWeapons = {
			["G36"] = true
		},
		EquippedWeapons = {
			Primary = nil,
			Secondary = nil,
			Melee = nil
		},
	}
]]

--=====================Equipping and stuff==========================
	
	local equipEvent = rs.WeaponSystemEvents:WaitForChild("Equip")
	
	equipEvent.OnServerEvent:Connect(function(player, wpn)
		warn("fired!", wpn)
		for key, info in pairs(weaponPrices) do
			if wpn == info.name then
				AddToEquipped(wpn, info.class)
				print("equipped", wpn, "and inserted to", weaponTable.EquippedWeapons)
			else
				warn("no such a weapon!")
			end
		end
	end)

I’d appreciate any help provided! :hidere: And if I missed some information. please let me know! Also I don’t mind some additional help for scripts themselves!

3 Likes

The issue is that the MouseButton1Click for EquipButton is connected for every weapon (7 in total). This means that the code will run 7 times for every press.

In order to fix it, I would recommend a variable which holds the most recently selected weapon in the UI, which is then accessed by the EquipButton.MouseButton1Click. Additionally, you should move the EquipButton mouse button click code outside of the newTypeButton.MouseButton1Click, so that it only runs once.

(If this doesn’t make sense, lmk and I can try to reword it - it’s a large block of text so I may have written it weirdly)

3 Likes

The click function works only once, I dunno why THAT particular “fire an event” function duplicates.

Alright, I’ll try later in the day, I hope it’s gonna work. Thank you in advance!

2 Likes

You told the game how to handle the click function (for EquipButton) multiple times, without removing any previous ways to handle it. Because of this, it will run each version of the handling every time the button is clicked, causing the issue

2 Likes

add “return” or “break” command after you fire em

2 Likes

The reason why it fires mutliple times is because of the event you connect in the function.
You should use :once() to fix this.

2 Likes

I don’t believe that that would fix it - all that changing it to Once will do is make all 7 functions fire still, but only do it for a single mouse click (all subsequent clicks are ignored by all functions), so still not the correct behaviour.

That will at best make it so a single item works, but no others (as the loop handles all items, but ending it with return or break would just make it handle the first item) - not the intended behaviour

2 Likes

How so? Once automatically disconnects once its been used.
I have come up with this and in my case it worked

1 Like

The issue in the original post is that they are connecting multiple functions to the same input, within a loop, which is causing the event to fire multiple times. Regardless of if they use Connect or Once, the functions are still connected to the mouse click event. In the case of Once - by the time it’s been disconnected, the remote event will have already been fired multiple times.

(We can continue in DMs if you want - better to keep the initial thread on topic)

2 Likes

So I tried this and nothing changed, it behaves exactly like it was before :sad:

umm, well I figured out where was the problem… It wasn’t even on the Client, but on the Server, there was an extra check which was always true…

I’m sorry for taking your time, thank you anyway for help! If I didn’t check your suggestions, I’d be stuck there forever.

I dunno who do I put the solution mark :sweat_smile:

1 Like

Since you found the solution, give it to yourself.

There’s nothing wrong with giving yourself the solution if you found it, because solutions aren’t to be used as a metric of credibility—they’re to be used for future readers to understand what reply was the solution.

1 Like

Alright then! Sounds pretty fair I guess

1 Like