How do I stop a function from multiplying itself multiple times after getting called?

How do I stop a function from multiplying itself multiple times after getting called?

So this is the bug I am trying to fix, every time I click a key on the keyboard using UIS the script written onto it keeps repeating for some reason.

Cause the thing is, I am making this script for a camera script and it seems to be repeating after calling it so many times.

Here is the output after I click the key once:

image

And this is the output after I click it 3 times:

image

Here’s the script:

local plr = game.Players.LocalPlayer

local cam = workspace.CurrentCamera

local ts = game:GetService("TweenService")
local UIS = game:GetService("UserInputService")
local m = plr:GetMouse()

-- Variables
local equipped, zoomTog = false, false

-- Tweens
local zoomIn = ts:Create(cam, TweenInfo.new(1, Enum.EasingStyle.Linear), {FieldOfView = 10})
local zoomOut = ts:Create(cam, TweenInfo.new(1, Enum.EasingStyle.Linear), {FieldOfView = 55})

-- Zooming
UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Z then
		if equipped == false then
			equipped = true
			m.Button2Down:Connect(function()
				if equipped == true then
					print("Equipped")
					if zoomTog == false then
						zoomTog = true
						UIS.InputBegan:Connect(function(input)
							if input.KeyCode == Enum.KeyCode.Y then
								if zoomTog == true then
									zoomIn:Play()
								end
							end
						end)
						UIS.InputEnded:Connect(function(input)
							if input.KeyCode == Enum.KeyCode.Y then
								if zoomTog == true then
									zoomIn:Pause()
								end
							end
						end)
						-- zoomingOut
						UIS.InputBegan:Connect(function(input)
							if input.KeyCode == Enum.KeyCode.T then
								if zoomTog == true then
									zoomOut:Play()
								end
							end
						end)
						UIS.InputEnded:Connect(function(input)
							if input.KeyCode == Enum.KeyCode.T then
								if zoomTog == true then
									zoomOut:Pause()
								end
							end
						end)
					elseif zoomTog == true then
						zoomTog = false
					end
				end
			end)
		elseif equipped == true then 
			equipped = false
		end
	end
end)

I’m sorry that it’s messy.

You should practice disconnecting your connections, they stack up

Ex: Rough code not tested, showing to disconnect the button 2 done on equip and unequip.

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Z then
		if equipped == false then
			equipped = true
if connection  then
connection:Disconnect()
connection = nil
end
			connection = m.Button2Down:Connect(function()
		elseif equipped == true then 
			equipped = false
connection:Disconnect()
connection  = nil
		end
	end
end)

Same applies to the other input ended and input began code.

Also I recommend checking out context action services because you can check for input began and input ended for a single keycode, no need for both began and ended connections.

2 Likes

Thank you so much! I’ve been waiting for so long lol thanks!

1 Like

You could also try adding a cooldown and if it dosent work disiable the script for X amout of time then it gets enabled again. (Idk if it works just my thoghts tho but could be worth a try ig)

2 Likes

Sorry but the script has already been solved, thanks for replying though.

1 Like