How to fix stats not updating when weapon changes?

Hi Developers! :wave:

I’ve made a system to show weapon stats like damage, fire rate and etc. But for some reason hit multipliers aren’t updating when a new weapon is selected? everything else changes to corresponding values.

Maybe it’s because these multipliers are contained in a table? I’d appreciate any help provided. Also if I need to clarify some things, please let me know! :hidere:

Here’s a LocalScript in StarterGUI:

Click me!
for i, weaponClass in ipairs(weaponClasses) do
	
	local newButton = script.WeaponClassButton:Clone()
	newButton.Text = weaponClass.Name
	
	newButton.Parent = loadout.WeaponClasses
	
	newButton.MouseButton1Click:Connect(function()
		
		for i, child in pairs(loadout.TypesList:GetChildren()) do

			if child:IsA("TextButton") then child:Destroy() end
		end
		
		for i, child in pairs(loadout.WeaponsList:GetChildren()) do

			if child:IsA("TextButton") then child:Destroy() end
		end

		
		for i, weaponType in ipairs(weaponClass:GetChildren()) do
			
			local newTypeButton = script.WeaponTypeButton:Clone()
			newTypeButton.Text = weaponType.Name

			newTypeButton.Parent = loadout.TypesList
			
			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.Parent = loadout.WeaponsList
					

					newWeaponButton.MouseButton1Click:Connect(function()
						
						local StatsModule = require(weaponChild:WaitForChild("Setting"):WaitForChild("1"))
						local SettingModule = require(weaponChild:WaitForChild("Setting"))
						
						local damage = StatsModule.BaseDamage
						local firerate = StatsModule.FireRate
						local reload = StatsModule.ReloadTime
						local magcapacity = StatsModule.AmmoPerMag
						local spread = StatsModule.Spread
						local bulletspeed = StatsModule.BulletSpeed
						local walkspeed = SettingModule.WalkSpeed
						
						local statgui = script.Parent.Parent.StatsFrame
						local basicstats = statgui.Container.StatTypeFrame.BasicStats
						local advancedstats = statgui.Container.StatTypeFrame.AdvancedStats
						
						basicstats.DamageLabel.Text = "Damage per bullet: " .. damage  --dmg
						basicstats.FireRateLabel.Text = "Fire rate: " .. firerate .."s" -- firerate
						basicstats.RldSpeedLabel.Text =	"Reload time: " .. reload .."s" -- reload
						basicstats.MagCapacityLabel.Text = "Mag capacity: " .. magcapacity  -- mag
						basicstats.SpreadLabel.Text = "Spread: " .. spread .. "°"  -- spread
						basicstats.BulletSpeedLabel.Text = "Bullet speed: " .. bulletspeed .." studs/s"  -- bullet speed
						basicstats.WalkSpeedLabel.Text = "Walk speed when equipped: " .. walkspeed .." studs/s" -- walk speed
						
						--indicating hit multipliers
--THAT'S THE PART WHICH DOESN'T WORK PROPERLY-----------------------------------------

						local humhitFrame = statgui.Parent.AdvancedPlus.HumHitMap
						
						local headframe = humhitFrame.HeadFrame
						local torsoframe = humhitFrame.TorsoFrame
						local rightarmframe = humhitFrame.RightArmFrame
						local leftarmframe = humhitFrame.LeftArmFrame
						local rightlegframe = humhitFrame.RightLegFrame
						local leftlegframe = humhitFrame.LeftLegFrame
						
						
						local multiplierTable = StatsModule.DamageMultipliers
						
						local headmulti = multiplierTable["Head"]
						local torsomulti = multiplierTable["Torso"]
						local rightarmMulti = multiplierTable["Right Arm"]
						local leftarmMulti = multiplierTable["Left Arm"]
						local rightlegMulti = multiplierTable["Right Leg"]
						local leftlegMulti = multiplierTable["Left Leg"]
				
						
						headframe.MouseEnter:Connect(function()
							humhitFrame.LabelDesc.Text = "Hit multiplier: " .. headmulti
						end)
						
						torsoframe.MouseEnter:Connect(function()
							humhitFrame.LabelDesc.Text = "Hit multiplier: " .. torsomulti
						end)
						
						rightarmframe.MouseEnter:Connect(function()
							humhitFrame.LabelDesc.Text = "Hit multiplier: " .. rightarmMulti
						end)
						
						leftarmframe.MouseEnter:Connect(function()
							humhitFrame.LabelDesc.Text = "Hit multiplier: " .. leftarmMulti
						end)
						
						rightlegframe.MouseEnter:Connect(function()
							humhitFrame.LabelDesc.Text = "Hit multiplier: " .. rightlegMulti
						end)
						
						leftlegframe.MouseEnter:Connect(function()
							humhitFrame.LabelDesc.Text = "Hit multiplier: " .. leftlegMulti
						end)
						
						--headframe.MouseLeave:Connect(function() humhitFrame.LabelDesc.Text = "Hit multipliers:" end)
						updateLoadout()
						
					end)
				end
			end)	
		end
	end)
end
2 Likes

You’re connecting .MouseEnter every time a new weapon is selected — meaning the first set of connections stick, and new multipliers don’t update because the old event handlers are still bound and use stale values.

So when you select a new weapon, the labels update, but the event handlers still refer to the old multipliers that were captured in the closures.

You need to do one of the following:

  1. Clear any previous connections before creating new ones.

  2. Or, better: connect the MouseEnter events only once, and update a shared currentMultiplierTable each time a new weapon is selected.

Here is a version using the second method.

Put this near the top of your script:
local currentMultiplierTable = {}

Then update the MouseEnter connections once, outside the button click (so they don’t duplicate):


local function updateHitLabel(partName)
	humhitFrame.LabelDesc.Text = "Hit multiplier: " .. (currentMultiplierTable[partName] or "N/A")
end

humhitFrame.HeadFrame.MouseEnter:Connect(function() updateHitLabel("Head") end)
humhitFrame.TorsoFrame.MouseEnter:Connect(function() updateHitLabel("Torso") end)
humhitFrame.RightArmFrame.MouseEnter:Connect(function() updateHitLabel("Right Arm") end)
humhitFrame.LeftArmFrame.MouseEnter:Connect(function() updateHitLabel("Left Arm") end)
humhitFrame.RightLegFrame.MouseEnter:Connect(function() updateHitLabel("Right Leg") end)
humhitFrame.LeftLegFrame.MouseEnter:Connect(function() updateHitLabel("Left Leg") end)

Now inside your weapon button MouseButton1Click, just update the shared currentMultiplierTable:
currentMultiplierTable = StatsModule.DamageMultipliers

This should work… I hope lol

2 Likes

Ok I will try it tomorrow and put a solution mark if it works, don’t have much time for today. Thank you in advance!

1 Like

It worked! I didn’t fully test it though, this topic mightt update if this won’t work properly at some point. HUGE Thanks to You!

Btw there’s the

Remade script
local currentMultiplierTable = {}


for i, weaponClass in ipairs(weaponClasses) do
	
	local newButton = script.WeaponClassButton:Clone()
	newButton.Text = weaponClass.Name
	
	newButton.Parent = loadout.WeaponClasses
	
	newButton.MouseButton1Click:Connect(function()
		
		
		for i, child in pairs(loadout.TypesList:GetChildren()) do

			if child:IsA("TextButton") then child:Destroy() end
		end
		
		for i, child in pairs(loadout.WeaponsList:GetChildren()) do

			if child:IsA("TextButton") then child:Destroy() end
		end

		
		for i, weaponType in ipairs(weaponClass:GetChildren()) do
			
			local newTypeButton = script.WeaponTypeButton:Clone()
			newTypeButton.Text = weaponType.Name

			newTypeButton.Parent = loadout.TypesList
			
			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.Parent = loadout.WeaponsList
					
					local StatsModule = require(weaponChild:WaitForChild("Setting"):WaitForChild("1"))
					local statgui = script.Parent.Parent.StatsFrame

					newWeaponButton.MouseButton1Click:Connect(function()
						
						
						local SettingModule = require(weaponChild:WaitForChild("Setting"))
						
						local damage = StatsModule.BaseDamage
						local firerate = StatsModule.FireRate
						local reload = StatsModule.ReloadTime
						local magcapacity = StatsModule.AmmoPerMag
						local spread = StatsModule.Spread
						local bulletspeed = StatsModule.BulletSpeed
						local walkspeed = SettingModule.WalkSpeed
						
						
						local basicstats = statgui.Container.StatTypeFrame.BasicStats
						local advancedstats = statgui.Container.StatTypeFrame.AdvancedStats
						
						basicstats.DamageLabel.Text = "Damage per bullet: " .. damage  --dmg
						basicstats.FireRateLabel.Text = "Fire rate: " .. firerate .."s" -- firerate
						basicstats.RldSpeedLabel.Text =	"Reload time: " .. reload .."s" -- reload
						basicstats.MagCapacityLabel.Text = "Mag capacity: " .. magcapacity  -- mag
						basicstats.SpreadLabel.Text = "Spread: " .. spread .. "°"  -- spread
						basicstats.BulletSpeedLabel.Text = "Bullet speed: " .. bulletspeed .." studs/s"  -- bullet speed
						basicstats.WalkSpeedLabel.Text = "Walk speed when equipped: " .. walkspeed .." studs/s" -- walk speed
						
						
						currentMultiplierTable = StatsModule.DamageMultipliers
						
						--headframe.MouseLeave:Connect(function() humhitFrame.LabelDesc.Text = "Hit multipliers:" end)
						
						
						statgui.WeaponNameLabel.Text = weaponChild.Name
						
						if weaponType.Name == "Primary" then
							primary = weaponChild
							script.Parent.StrPrimary.Value = weaponChild.Name	

						elseif weaponType.Name == "Secondary" then
							secondary = weaponChild
							script.Parent.StrSecondary.Value = weaponChild.Name					

						elseif weaponType.Name == "Melee" then
							melee = weaponChild
							script.Parent.StrMelee.Value = weaponChild.Name
							
						end
						updateLoadout()
						
					end)
					--indicating hit multipliers

					local humhitFrame = statgui.Parent.AdvancedPlus.HumHitMap

					local headframe = humhitFrame.HeadFrame
					local torsoframe = humhitFrame.TorsoFrame
					local rightarmframe = humhitFrame.RightArmFrame
					local leftarmframe = humhitFrame.LeftArmFrame
					local rightlegframe = humhitFrame.RightLegFrame
					local leftlegframe = humhitFrame.LeftLegFrame	
					
					local function update(partName)
						humhitFrame.LabelDesc.Text = "Hit multiplier: " .. (currentMultiplierTable[partName] or "N/A")
					end
					
					headframe.MouseEnter:Connect(function() update("Head") end)--humhitFrame.LabelDesc.Text = "Hit multiplier: " .. headmulti

					torsoframe.MouseEnter:Connect(function()update("Torso") end)

					rightarmframe.MouseEnter:Connect(function()update("Right Arm") end)

					leftarmframe.MouseEnter:Connect(function()update("Left Arm") end)

					rightlegframe.MouseEnter:Connect(function()update("Right Leg") end)

					leftlegframe.MouseEnter:Connect(function()update("Left Leg") end)
				end
			end)	
		end
	end)
end

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