Lock on script breaks on reset

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

1 Like

Assuming that your script is in StarterPlayerScripts/Backpack (as you mentioned), you reference the character only the first time it exists.
When the player resets their character, your char variable becomes nil and so it does not work.

Try to put that local script in StarterCharacterScripts so that the scripts get re-run every time with the new character.
You can also reference the character more easily from StarterCharacterScripts:

Hope that helps :slight_smile:

2 Likes

so sorry for the late reply!

this worked! thank you!! and your explanation makes alot of sense idk why i didnt think of doing that :sweat_smile: