Function not disconnecting/Not Working?

I’m making a lock on system and this function is not working one bit
I looked at a bunch of post and none of them are similar to my situation.

spent 2 hrs trying to figure this out

script:

task.wait(2)
local CAS = game:GetService(“ContextActionService”)
local userSettings = UserSettings().GameSettings
local UIS = game:GetService(“UserInputService”)
local plr = game:GetService(“Players”).LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild(“Humanoid”)
local cam = game.Workspace.CurrentCamera
local Info = game:GetService(“ReplicatedStorage”):WaitForChild(“Info”)
local lockOn = game:GetService(“ReplicatedStorage”):WaitForChild(“LockOn”)
local Key = Enum.KeyCode.T
local distance = 25
local LockOn = “”
local LockedOnPlr = false
local Locked = nil

function GetNearestPlr(HRP)
	userSettings.RotationType = Enum.RotationType.CameraRelative
	game:GetService(“RunService”).Heartbeat:Connect(function()
		if LockOn ~= “” and LockedOnPlr == true then
			cam.CFrame = CFrame.lookAt(cam.CFrame.Position,HRP.Position)
		elseif LockOn == “” and LockedOnPlr == false then
			print(“test2”)
			–game:GetService(“RunService”).Heartbeat:Connect(GetNearestPlr):Disconnect()
		end
	end)
end

function UpdateHealthnHRP(hum,NewInfo,HRP,plrName,Head)

	while wait(0.1) do
		if LockedOnPlr == true and LockOn == plrName then
			NewInfo.Slider.Health.Text = "Health/"..math.round(hum.Health)
		end
	end
end

UIS.InputBegan:Connect(function(key,pro)
	if not pro then
		if key.KeyCode == Key and LockedOnPlr == false and LockOn == “” then
			for i, char in pairs(game.Workspace:GetChildren()) do
				local HRP = char:FindFirstChild(“HumanoidRootPart”)
				local Hum = char:FindFirstChild(“Humanoid”)
				local Head = char:FindFirstChild(“Head”)
				if HRP and hum then
					local Distance = (plr.Character.HumanoidRootPart.Position - HRP.Position).Magnitude
					if distance >= Distance and char ~= plr.Character then
						local plrName = char.Name
						LockedOnPlr = true
						LockOn = plrName
						local NewLockon = lockOn:Clone()
						NewLockon.Parent = HRP
						NewLockon.Adornee = HRP
						local NewInfo = Info:Clone()
						NewInfo.Parent = char.Head
						NewInfo.Adornee = char.Head
						NewInfo.Slider.PlrName.Text = "Name: "…char.Name
						NewInfo.Slider.Health.Text = "Health: "…Hum.Health
						print(LockOn)
						UpdateHealthnHRP(Hum,NewInfo,HRP,plrName,Head)
						Locked = game:GetService(“RunService”).Heartbeat:Connect(GetNearestPlr(HRP)) – function not working – this is the problem
							print(Locked)
							break
						end
					end
				end
			end
		elseif LockedOnPlr == true and LockOn ~= “” and key.KeyCode == Key then
			LockedOnPlr = false
			LockOn = “”
			print(“test1”)
		end
	end) – when the t key is pressed again and its true then reset camera pos

video:
(https://gyazo.com/e7bfee9af66375ab096f85fc04c6c1a4)

2 Likes

Please format the code, Its hard to read it as text. You can format code like this with 3 ` ← of that on the front and back.

This is for my own viewing:

task.wait(2)
local CAS = game:GetService(“ContextActionService”)
local userSettings = UserSettings().GameSettings
local UIS = game:GetService(“UserInputService”)
local plr = game:GetService(“Players”).LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild(“Humanoid”)
local cam = game.Workspace.CurrentCamera
local Info = game:GetService(“ReplicatedStorage”):WaitForChild(“Info”)
local lockOn = game:GetService(“ReplicatedStorage”):WaitForChild(“LockOn”)
local Key = Enum.KeyCode.T
local distance = 25
local LockOn = “”
local LockedOnPlr = false
local Locked = nil

function GetNearestPlr(HRP)
	userSettings.RotationType = Enum.RotationType.CameraRelative
	game:GetService(“RunService”).Heartbeat:Connect(function()
		if LockOn ~= “” and LockedOnPlr == true then
			cam.CFrame = CFrame.lookAt(cam.CFrame.Position,HRP.Position)
		elseif LockOn == “” and LockedOnPlr == false then
			print(“test2”)
			–game:GetService(“RunService”).Heartbeat:Connect(GetNearestPlr):Disconnect()
		end
	end)
end

function UpdateHealthnHRP(hum,NewInfo,HRP,plrName,Head)

	while wait(0.1) do
		if LockedOnPlr == true and LockOn == plrName then
			NewInfo.Slider.Health.Text = "Health/"..math.round(hum.Health)
		end
	end
end

UIS.InputBegan:Connect(function(key,pro)
	if not pro then
		if key.KeyCode == Key and LockedOnPlr == false and LockOn == “” then
			for i, char in pairs(game.Workspace:GetChildren()) do
				local HRP = char:FindFirstChild(“HumanoidRootPart”)
				local Hum = char:FindFirstChild(“Humanoid”)
				local Head = char:FindFirstChild(“Head”)
				if HRP and hum then
					local Distance = (plr.Character.HumanoidRootPart.Position - HRP.Position).Magnitude
					if distance >= Distance and char ~= plr.Character then
						local plrName = char.Name
						LockedOnPlr = true
						LockOn = plrName
						local NewLockon = lockOn:Clone()
						NewLockon.Parent = HRP
						NewLockon.Adornee = HRP
						local NewInfo = Info:Clone()
						NewInfo.Parent = char.Head
						NewInfo.Adornee = char.Head
						NewInfo.Slider.PlrName.Text = "Name: "…char.Name
						NewInfo.Slider.Health.Text = "Health: "…Hum.Health
						print(LockOn)
						UpdateHealthnHRP(Hum,NewInfo,HRP,plrName,Head)
						Locked = game:GetService(“RunService”).Heartbeat:Connect(GetNearestPlr(HRP)) – function not working – this is the problem
							print(Locked)
							break
						end
					end
				end
			end
		elseif LockedOnPlr == true and LockOn ~= “” and key.KeyCode == Key then
			LockedOnPlr = false
			LockOn = “”
			print(“test1”)
		end
	end) – when the t key is pressed again and its true then reset camera pos

oh i didnt know you could do that.

1 Like

solution solved, had the function in the wrong spot.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.