I don’t know how to word the title, but basically, I have this script that when I press F, it changes the field of view. I used this to make a primitive aim down sights system. I need help because I don’t know how to make it do the unaim lines of code when I press F again. Here’s my code:
‘’'Lua
–local scoped = false
local cam
script.Parent.Equipped:connect(function(mouse)
cam = game.Workspace.CurrentCamera
mouse.KeyDown:connect(function(key)
key = key:lower()
if key == "f" then
cam.FieldOfView = scoped and 70 or 51
scoped = not scoped
script.Parent.ADSGun.Transparency = 0
script.Parent.ADSSight1.Transparency = 0
script.Parent.ADSSight2.Transparency = 0
script.Parent.ADSSight3.Transparency = 0
script.Parent.FrontSight1.Transparency = 0
script.Parent.FrontSight2.Transparency = 0
script.Parent.FrontSight3.Transparency = 0
script.Parent.FrontSight4.Transparency = 0
script.Parent.FrontSight5.Transparency = 0
script.Parent.FrontSight6.Transparency = 0
script.Parent.WorldGun.Transparency = 1
script.Parent.WorldBackSight.Transparency = 1
script.Parent.FakeMag.Transparency = 1
script.Parent.AttachmentRail1.Transparency = 1
script.Parent.AttachmentRail2.Transparency = 1
script.Parent.WorldSight5.Transparency = 1
script.Parent.WorldSight4.Transparency = 1
script.Parent.WorldSight3.Transparency = 1
script.Parent.WorldSight2.Transparency = 1
script.Parent.WorldSight1.Transparency = 1
end
end)
end)
script.Parent.Unequipped:connect(function()
cam.FieldOfView = 70
script.Parent.ADSGun.Transparency = 1
script.Parent.ADSSight1.Transparency = 1
script.Parent.ADSSight2.Transparency = 1
script.Parent.ADSSight3.Transparency = 1
script.Parent.FrontSight1.Transparency = 1
script.Parent.FrontSight2.Transparency = 1
script.Parent.FrontSight3.Transparency = 1
script.Parent.FrontSight4.Transparency = 1
script.Parent.FrontSight5.Transparency = 1
script.Parent.FrontSight6.Transparency = 1
script.Parent.WorldGun.Transparency = 0
script.Parent.WorldBackSight.Transparency = 0
script.Parent.FakeMag.Transparency = 0
script.Parent.AttachmentRail1.Transparency = 0
script.Parent.AttachmentRail2.Transparency = 0
script.Parent.WorldSight5.Transparency = 0
script.Parent.WorldSight4.Transparency = 0
script.Parent.WorldSight3.Transparency = 0
script.Parent.WorldSight2.Transparency = 0
script.Parent.WorldSight1.Transparency = 0
end)
‘’’
The code starting from:
script.Parent.Unequipped:connect(function()
cam.FieldOfView = 70
is where I’m trying to make it do if it presses F again.
local uis = game:GetService("UserInputService") -- mouse.keydown is deprecated so you need userinputservice to check player inputs in the future
local cam = workspace.CurrentCamera
local scoped = false
script.Parent.Equipped:Connect(function()
uis.InputBegan:Connect(function(input,gpe)
if gpe then return end -- just make sure if you are chatting and pressing f the function should not run
if input.KeyCode == Enum.KeyCode.F then
scoped = not scoped
cam.FieldOfView = scoped and 70 or 50
script.Parent.ADSGun.Transparency = 0
script.Parent.ADSSight1.Transparency = 0
script.Parent.ADSSight2.Transparency = 0
script.Parent.ADSSight3.Transparency = 0
script.Parent.FrontSight1.Transparency = 0
script.Parent.FrontSight2.Transparency = 0
script.Parent.FrontSight3.Transparency = 0
script.Parent.FrontSight4.Transparency = 0
script.Parent.FrontSight5.Transparency = 0
script.Parent.FrontSight6.Transparency = 0
script.Parent.WorldGun.Transparency = 1
script.Parent.WorldBackSight.Transparency = 1
script.Parent.FakeMag.Transparency = 1
script.Parent.AttachmentRail1.Transparency = 1
script.Parent.AttachmentRail2.Transparency = 1
script.Parent.WorldSight5.Transparency = 1
script.Parent.WorldSight4.Transparency = 1
script.Parent.WorldSight3.Transparency = 1
script.Parent.WorldSight2.Transparency = 1
script.Parent.WorldSight1.Transparency = 1
end
end)
end)
script.Parent.Unequipped:connect(function()
cam.FieldOfView = 70
script.Parent.ADSGun.Transparency = 1
script.Parent.ADSSight1.Transparency = 1
script.Parent.ADSSight2.Transparency = 1
script.Parent.ADSSight3.Transparency = 1
script.Parent.FrontSight1.Transparency = 1
script.Parent.FrontSight2.Transparency = 1
script.Parent.FrontSight3.Transparency = 1
script.Parent.FrontSight4.Transparency = 1
script.Parent.FrontSight5.Transparency = 1
script.Parent.FrontSight6.Transparency = 1
script.Parent.WorldGun.Transparency = 0
script.Parent.WorldBackSight.Transparency = 0
script.Parent.FakeMag.Transparency = 0
script.Parent.AttachmentRail1.Transparency = 0
script.Parent.AttachmentRail2.Transparency = 0
script.Parent.WorldSight5.Transparency = 0
script.Parent.WorldSight4.Transparency = 0
script.Parent.WorldSight3.Transparency = 0
script.Parent.WorldSight2.Transparency = 0
script.Parent.WorldSight1.Transparency = 0
end) -- better put "script.Parent" into variable local x = script.Parent