Hello developers. So I made a script were if you press a bind the camera would focus on the ball that’s currently in air. But when I press Bind 1 on the keyboard It does it perfectly fine. But when I press the bind again to get out of focus mode it stays on focus mode. How can I fix this?
Code:
local UIS = game:GetService('UserInputService')
local Players = game:GetService('Players')
local Player = Players.LocalPlayer
local UsingCam = false
local Distance = -1
local Ball = nil
workspace.CurrentCamera.CameraType = 'Custom'
UIS.InputBegan:Connect(function(Key)
if Key.KeyCode == Enum.KeyCode.One then
UsingCam = true
local Carrier = Instance.new('Folder')
Carrier.Name = 'Carrier'
for _,V in pairs(workspace:GetDescendants()) do
if V.Name == 'Football' then
if V:IsA('Tool') then
local Handle = V:WaitForChild('Handle')
if Distance == -1 or (Player.Character.HumanoidRootPart.Position - V.Handle.Position).Magnitude < Distance then
Distance = (Player.Character.HumanoidRootPart.Position - V.Position).Magnitude
Ball = V
workspace.CurrentCamera.CameraSubject = V
end
else
if Distance == -1 or (Player.Character.HumanoidRootPart.Position - V.Position).Magnitude < Distance then
Distance = (Player.Character.HumanoidRootPart.Position - V.Position).Magnitude
Ball = V
workspace.CurrentCamera.CameraSubject = V
end
end
end
end
end
end)
UIS.InputBegan:Connect(function(Key)
if Key.KeyCode == Enum.KeyCode.One then
if Player.Backpack:FindFirstChild('Carrier') then
Player.Backpack.Carrier:Destroy()
end
UsingCam = false
Ball = nil
Distance = -1
workspace.CurrentCamera.CameraSubject = Player.Character.Humanoid
end
end)
workspace.DescendantAdded:Connect(function(Child)
if UsingCam then
if Ball == nil then
if Child.Name == 'Football' then
if Child:IsA('Tool') then
local Handle = Child:WaitForChild('Handle')
Ball = Handle
workspace.CurrentCamera.CameraSubject = Handle
else
Ball = Child
workspace.CurrentCamera.CameraSubject = Child
end
end
end
end
end)
workspace.DescendantRemoving:Connect(function(DEC)
if UsingCam then
if DEC == Ball then
for _,V in pairs(workspace:GetDescendants()) do
if V.Name == 'Football' then
if V:IsA('Tool') then
local Handle = V:WaitForChild('Handle')
if Distance == -1 or (Player.Character.HumanoidRootPart.Position - V.Handle.Position).Magnitude < Distance then
Distance = (Player.Character.HumanoidRootPart.Position - V.Handle.Position).Magnitude
Ball = Handle
workspace.CurrentCamera.CameraSubject = V.Handle
end
else
if Distance == -1 or (Player.Character.HumanoidRootPart.Position - V.Position).Magnitude < Distance then
Distance = (Player.Character.HumanoidRootPart.Position - V.Position).Magnitude
Ball = V
workspace.CurrentCamera.CameraSubject = V
end
end
end
end
end
end
end)
workspace.ChildRemoved:Connect(function(Child)
if UsingCam then
if Child == Ball then
Ball = nil
end
end
end)