Hello Devs, So when I press Bind one of the keyboard it works perfectly fine WHEN the balls parent is the workspace. But if the player spawns a ball or its parented inside the character nothing happens when I press bind one and I am completely clueless on what is happening. Please help.
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
if not UsingCam 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 - Handle.Position).Magnitude < Distance then
Distance = (Player.Character.HumanoidRootPart.Position - Handle.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
else
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
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)