RemoteEvent server saving multiple objects help

Hi, I was writing a shop GUI and got stuck with some problems.

function showInfo(Weapon)
	--//Ran Variables
	local StatsFolder = Weapon:FindFirstChild("Stats")
	local AccuracyV = StatsFolder:FindFirstChild("Accuracy").Value
	local AmmoV = StatsFolder:FindFirstChild("Ammo").Value
	local DamageV = StatsFolder:FindFirstChild("Damage").Value
	local FireRateV = StatsFolder:FindFirstChild("FireRate").Value
	
	InfoFrame.Accuracy.Text = "Accuracy: "..AccuracyV
	InfoFrame.Ammo.Text = "Ammo: "..AmmoV
	InfoFrame.Damage.Text = "Damage: "..DamageV
	InfoFrame.FireRate.Text = "FireRate: "..FireRateV
	
	
	InfoFrame.Visible = true
	
	InteractButton.MouseButton1Click:Connect(function()
		ShopRemote:FireServer(Weapon)
	end)
	
end


local function hideInfo()
	InfoFrame.Visible = false
end





for i, v in pairs(BothFrame:GetChildren()) do
	if v:IsA("ViewportFrame") then

		
		
		--//WhenEnterAndLeave
		v.MouseEnter:Connect(function()
			print(v)
			showInfo(v)
		end)
		v.MouseLeave:Connect(function()
			print(v)
		end)
	end
end

Here is the code on the server


ShopRemote.OnServerEvent:Connect(function(plr,Weapon)
	print(Weapon.NameText.Text)
end)

So the problem is, the event saves how many times the mouse has entered the viewportframe.
So if you enter your mouse in scar’s portframe 3 times, it would fire 3 times etc.
https://gyazo.com/741566503da452288a0b38f3554747e6

as you can see, how many times it prints on the server is depending on how many times the mouse enter on client.

I know this problem is probably very easy to solve, but I can’t.

I’m not sure what the problem here is. Are you wanting it to where the remote only fires for the first time you hover your mouse over a weapon’s UI frame? And not firing for each subsequent hover-over.

Yea, I want it to fire only once the mouse is hovered and when it goes to the other viewport, it only fires that one specific one.

But to clarify, you don’t want it to fire again if you hover over the same frame a second or third time?

Exactly what I’m trying to say. 3O CHARRR

This should work. All we do is create a table on the client, and whenever the mouse enters the frame, it checks if the table has an entry for that frame. If not, it creates an entry and runs showInfo. If it does have an entry, it doesn’t do anything. Technically you could just have the script disconnect the MouseEnter connection after the first time it runs but that’s a little bit more work.

function showInfo(Weapon)
	--//Ran Variables
	local StatsFolder = Weapon:FindFirstChild("Stats")
	local AccuracyV = StatsFolder:FindFirstChild("Accuracy").Value
	local AmmoV = StatsFolder:FindFirstChild("Ammo").Value
	local DamageV = StatsFolder:FindFirstChild("Damage").Value
	local FireRateV = StatsFolder:FindFirstChild("FireRate").Value
	
	InfoFrame.Accuracy.Text = "Accuracy: "..AccuracyV
	InfoFrame.Ammo.Text = "Ammo: "..AmmoV
	InfoFrame.Damage.Text = "Damage: "..DamageV
	InfoFrame.FireRate.Text = "FireRate: "..FireRateV
	
	
	InfoFrame.Visible = true
	
	InteractButton.MouseButton1Click:Connect(function()
		ShopRemote:FireServer(Weapon)
	end)
	
end


local function hideInfo()
	InfoFrame.Visible = false
end


local Frames = {}

for i, v in pairs(BothFrame:GetChildren()) do
	if v:IsA("ViewportFrame") then
		--//WhenEnterAndLeave
		v.MouseEnter:Connect(function()
			print(v)
            if not Frames[v] then
                Frames[v] = true
                showInfo(v)
            end
		end)
		v.MouseLeave:Connect(function()
			print(v)
		end)
	end
end

I know what the issue is here.

You’re making the Mouse Button Click connection in showInfo(),

So for each time that your mouse enters the frame, you’re making a new connection.

1 Like

Ohh, I see what you mean, what is the way to fix that? I was originally gonna make viewportframe click, but then I realised there is no click event for it.

You can try to create an invisible button with +1 ZIndex above the viewport frame, and just use that as your button.

But I want the equip button to be the one that fires it

It did not work by the way. As @OldGoldie said, it’s with the double connection.

Hmm OldGoldie is right, I didn’t notice that. But this should still be solved by my code because showInfo only gets run once per frame.

The script you modified fixed the one time issue. The problem still occurs when the mouse enters the different weapon for example if it enters scars twice, it only prints scar once. But if it enters scar and other weapon for example m1014, it prints both of them which means it still fires both.

Guys, I fixed it. The problem was actually with the mouseenter event it self. It’s pretty scuffed, so I ended up making an invisible button like he said, everything is fixed now.