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:
And this is the output after I click it 3 times:
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.