Text doubling issue

Hey all,
I’ve been stuck on this bug of mine for a bit which I’m sure is easy to fix, but I just don’t know what to look up to fix it. So I was wondering if anyone on the forum could help.

What’s happening is that whenever I press E to open up a Gui, the next time I open it up, the numbers add up by 1 digit. I think I need to stop/break or disconnect something but I don’t know what. Any and all help would be really appreciated.

Here’s the issue in a Gif:
https://gyazo.com/c7b1a6d9e0a384ee9d4a0a08d74a6aef

Local script:

local atms = game.Workspace.ATM_Interacts
local inputService = game:GetService("UserInputService")
local repStorage = game:GetService("ReplicatedStorage")
local firedValue = repStorage.temp.Fired.Value


--// ATM System

local firedValue = false
inputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		for _, atm in pairs(atms:GetChildren()) do
			local mag = (atm.Position - plyr.Character:WaitForChild("HumanoidRootPart").Position).Magnitude
			
			if mag <= atm.Range.Value and firedValue == false then
				repStorage.ATMevents.ATMinteract:FireServer()
				firedValue = true
			elseif firedValue == true then
				print("Fired value didn't fire because it's true!")
			end
		end
	end
end)

Server script:

local plyrs = game:GetService("Players")
local repStorage = game:GetService("ReplicatedStorage")
local firedValue = repStorage.temp.Fired.Value


repStorage.ATMevents.ATMinteract.OnServerEvent:Connect(function(plyr)
	local plyrATMgui = plyr.PlayerGui.atmGui
	plyrATMgui.Enabled = true
	
	for _, key in pairs(plyrATMgui.atmBackground.Keys:GetChildren()) do
		key.MouseButton1Click:Connect(function()
			plyrATMgui.atmBackground.cashAmountFrame.cashAmountText.Text = plyrATMgui.atmBackground.cashAmountFrame.cashAmountText.Text..key.Text
		end)
	end
		
	plyrATMgui.atmBackground.closeButton.MouseButton1Click:Connect(function()
		plyrATMgui.atmBackground.cashAmountFrame.cashAmountText.Text = "$"
		plyrATMgui.Enabled = false
		firedValue = false
	end)
end)
1 Like

By the looks of it, you’re detecting when the player’s clicking the buttons server-side. This in itself isn’t recommended - but what’s causing the issue you’re seeing is that every time the player opens the UI:

repStorage.ATMevents.ATMinteract.OnServerEvent:Connect(function(plyr)

the server’s setting up a new listener for when they click the button:

key.MouseButton1Click:Connect(function()

You’ll either want to disconnect the above listener when the player closes the UI, or detect button presses client-side instead.

4 Likes

It looks like you did accept transition but you forgot to clear out the textbox. (sorry idk how to script)

1 Like

I changed it up to detect button presses client-side side, but I am still getting the same issue. Did I overlook something or?

Local script:

local plyr = game:GetService("Players").LocalPlayer
local atms = game.Workspace.ATM_Interacts
local inputService = game:GetService("UserInputService")
local repStorage = game:GetService("ReplicatedStorage")
local firedValue = repStorage.temp.Fired.Value


--// Functions

local function interactAtm()
	script.Parent.atmGui.Enabled = true
	
	for _, key in pairs(script.Parent.atmGui.atmBackground.Keys:GetChildren()) do
		key.MouseButton1Down:Connect(function()
			script.Parent.atmGui.atmBackground.cashAmountFrame.cashAmountText.Text = script.Parent.atmGui.atmBackground.cashAmountFrame.cashAmountText.Text..key.Text
			print("Are you printing more?")
		end)
	end
end

--

script.Parent:WaitForChild("atmGui").atmBackground.closeButton.MouseButton1Click:Connect(function()
	script.Parent.atmGui.atmBackground.cashAmountFrame.cashAmountText.Text = "$"
	script.Parent.atmGui.Enabled = false
	firedValue = false
	print("Or you?")
end)

--// ATM System

local firedValue = false
inputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		for _, atm in pairs(atms:GetChildren()) do
			local mag = (atm.Position - plyr.Character:WaitForChild("HumanoidRootPart").Position).Magnitude
			
			if mag <= atm.Range.Value and firedValue == false then
				firedValue = true
				interactAtm()
			elseif firedValue == true then
				print("Fired value didn't fire because it's true!")
			end
		end
	end
end)
2 Likes

You’re still setting up new listeners for button presses every time the player opens the UI - I recommend moving this:

	for _, key in pairs(script.Parent.atmGui.atmBackground.Keys:GetChildren()) do
		key.MouseButton1Down:Connect(function()
			script.Parent.atmGui.atmBackground.cashAmountFrame.cashAmountText.Text = script.Parent.atmGui.atmBackground.cashAmountFrame.cashAmountText.Text..key.Text
			print("Are you printing more?")
		end)
	end

out of the interactAtm function and just having it standalone. Put an ‘if’ statement in there to check if the UI is visible if that’s a concern, and clear the number display when it’s opened/closed.

tl;dr: Running key.MouseButton1Down:Connect(function() multiple times doesn’t void the previous event listener, and however many times you run it is however many times the code inside it will run when that event fires.

1 Like

Ohh, okay I think I understand. So calling this function multiple times doesn’t get rid of the previous listeners as you said, therefore it just keeps piling up, so how would I disconnect the previous listener?

I thought :Disconnect() disconnects the function permanently

2 Likes

I’d recommend getting rid of the interactAtm function entirely and moving the ‘for’ loop outside it - you only need to run that once to set up MouseButton1Down event listeners. Instead of calling interactAtm, just have atmGui.Enabled = true.

For future reference, though, if you want to disconnect an event, you can use :Disconnect() like so:

local listener = key.MouseButton1Down:Connect(function()
    -- do stuff here
end)

listener:Disconnect()

Disconnecting a listener only disconnects that instance of it - any other times you’ve called it will remain connected unless you call it on those too.

1 Like

Wow, it seems so simple in hindsight lol. Nonetheless thank you so much for the help and teaching me some new things :smiley:

2 Likes