I am trying o create a script that gets the dummy closest to the players cursor, and checks if any parts are in the way. The problem is when holding down R it locks but only for a couple seconds before unlocking and then it breaks spinning the camera around like crazy
Video:
robloxapp-20210718-2247143.wmv (2.1 MB)
Here is my code I can’t find anything wrong with it:
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local CurrentCamera = workspace.CurrentCamera
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Mouse = LocalPlayer:GetMouse()
local GetClosestPlayer = function()
local Player = nil
local Distance = math.huge
for _, Value in ipairs(workspace.Dummies:GetChildren()) do
if Value ~= LocalPlayer then
local Character = Value
if Character then
local Head = Character:FindFirstChild("Head")
if Head then
if table.getn(CurrentCamera:GetPartsObscuringTarget({Head.CFrame.Position}, Character:GetDescendants())) == 0 then
local Position, _ = CurrentCamera:WorldToScreenPoint(Head.Position)
local Magnitude = (Vector2.new(Position.X, Position.Y) - Vector2.new(Mouse.X, Mouse.Y)).Magnitude
if Magnitude < Distance then
Player = Value
Distance = Magnitude
end
end
end
end
end
end
return Player
end
RunService.RenderStepped:Connect(function()
local ClosestPlayer = GetClosestPlayer()
if ClosestPlayer and UserInputService:IsKeyDown(Enum.KeyCode.R) then
CurrentCamera.CFrame = CFrame.lookAt(CurrentCamera.CFrame.Position, ClosestPlayer.Head.CFrame.Position)
end
end)
I would really appreciate the help because I do not know how to do this