Displaying Names and Ammo Amount

Ok so i have this gun that displays its name and its ammo ammount. The problem is when i have different guns with the same script inside it gets weird and tries to put both names in it and so it just flickers. I found the problem, its in the code below. It is ebcause they wait(0) aand try to find the “GunHud” so how would i fix it so only the equiped gun will show its name and amount?

while wait(0) do
	if game.Players.LocalPlayer.PlayerGui:FindFirstChild("GunHUD") then
		game.Players.LocalPlayer.PlayerGui:FindFirstChild("GunHUD").GunName.Text = script.Parent.Name
		game.Players.LocalPlayer.PlayerGui:FindFirstChild("GunHUD").CurrentAmmo.Text = Settings.CurrentAmmo.Value.."/"..Settings.MagSize.Value
		print(Settings.CurrentAmmo.Parent.Parent.Name)
	end
	if state.autoShooting and state.equipped and Coroutine.Value == false and not isReloading then
		handleShot()
	end
end


This would most likely be due to the while do running the background, it doesn’t matter if its disabled etc…

You should rather instead make a script inside the UI possible, and for the name get the tool that they are holding from the character. Character:FindFirstChildOfClass("Tool")

This would also be the best practice for your script aswell. (To my knowledge)

local Players = game:GetService("Players")

local Player = Players.LocalPlayer
local PlayerGui = Player.PlayerGui

local GunHUD = PlayerGui:WaitForChild("GunHUD")

while true do
	task.wait()
	
	GunHUD.GunName.Text = script.Parent.Name
	GunHUD.CurrentAmmo.Text = `{Settings.CurrentAmmo.Value} / {Settings.MagSize.Value}`
	print(Settings.CurrentAmmo.Parent.Parent.Name)
	
	if state.autoShooting and state.equipped and Coroutine.Value == false and not isReloading then
		handleShot()
	end
end