I was looking at tutorials to get an idea of where to start creating my lock on script for my game. I found a script that looked promising so i rewrote it and it works! but the issue is the script wont run when the player resets. I tried putting it in starter player scripts and the backpack but neither of those worked. I tried to debug it and there are no errors.
heres the code
--------------------------------
--GHet Variables
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local HRP = char:WaitForChild("HumanoidRootPart")
local UIS = game:GetService("UserInputService")
local RS = game:GetService("RunService")
local camera = game.Workspace.CurrentCamera
local mouse = player:GetMouse()
local camPart = Instance.new("Part",camera)
local BP = Instance.new("BodyPosition",camPart)
local target
local distance
local TS = game:GetService("TweenService")
--------------------------------
--Instance configuration
camPart.Size = Vector3.new(1,1,1)
camPart.Transparency = 1
camPart.CanCollide = false
camPart.CanTouch = false
----
BP.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
BP.D = 500
--------------------------------
--User Input
print("no")
UIS.InputBegan:Connect(function(input, chat)
if chat then return end --If player is typing code will not run
if input.UserInputType == Enum.UserInputType.MouseButton3 then --If middle mouse button is pressed
if mouse.Target and mouse.Target.Parent:FindFirstChildOfClass("Humanoid") then --If the mouse target has a humanoid child then
print("working")
target = mouse.Target.Parent
local camPosition = CFrame.new(HRP.Position, target.PrimaryPart.Position) * CFrame.new(0,4,9).Position --cameraPosition is located at the HRP, While facing the targets primary part
BP.Position = camPosition
camPart.Position = CFrame.new(HRP.Position, target.PrimaryPart.Position) * CFrame.new(0,4,9).Position
else --If no humanoid then
target = nil
end
end
end)
RS.RenderStepped:Connect(function(fps)
if target and (HRP.Position - target.PrimaryPart.Position).Magnitude <= 50 and target:WaitForChild("Humanoid") ~= nil and target:WaitForChild("Humanoid").Health ~= 0 and char:WaitForChild("Humanoid") ~= nil and char:WaitForChild("Humanoid").Health ~= 0 then
local camPosition = CFrame.new(HRP.Position, target.PrimaryPart.Position) * CFrame.new(0,4,9).Position --cameraPosition is located at the HRP, While facing the targets primary part
camera.CameraType = Enum.CameraType.Scriptable
TS:Create(camera, TweenInfo.new(.5),{CFrame = CFrame.new(HRP.Position, target.PrimaryPart.Position) * CFrame.new(0,4,11)}):Play()
TS:Create(HRP, TweenInfo.new(.5),{CFrame = CFrame.new(HRP.Position, target.PrimaryPart.Position)}):Play()
BP.Position = camPosition
else
BP.Parent = nil
target = nil
camera.CameraType = Enum.CameraType.Custom
end
end)
--was taking notes of the code i wrote, haha
I narrowed down the problem to around here
RS.RenderStepped:Connect(function(fps)
if target and (HRP.Position - target.PrimaryPart.Position).Magnitude <= 50 and target:WaitForChild("Humanoid") ~= nil and target:WaitForChild("Humanoid").Health ~= 0 and char:WaitForChild("Humanoid") ~= nil and char:WaitForChild("Humanoid").Health ~= 0 then
local camPosition = CFrame.new(HRP.Position, target.PrimaryPart.Position) * CFrame.new(0,4,9).Position --cameraPosition is located at the HRP, While facing the targets primary part
camera.CameraType = Enum.CameraType.Scriptable
TS:Create(camera, TweenInfo.new(.5),{CFrame = CFrame.new(HRP.Position, target.PrimaryPart.Position) * CFrame.new(0,4,11)}):Play()
TS:Create(HRP, TweenInfo.new(.5),{CFrame = CFrame.new(HRP.Position, target.PrimaryPart.Position)}):Play()
BP.Position = camPosition
else
BP.Parent = nil
target = nil
camera.CameraType = Enum.CameraType.Custom
end
end)
as shown in the video the lock on doesnt work after reset. im not sure what exactly the issue is so help would greatly be appreciated. Thank you for your time