Anything I can do to make my targeting system look nice?

I have made a script that allows the player to target characters withing 40 studs of range. It is not finished yet, since I haven’t coded in any enemies. And it doesn’t know if the enemy is dead, because I havent coded enemies.

Thinking of using tweening, but not sure about the target position of the camera tho
Here is my script if it needs some reworking:

local camera = workspace.CurrentCamera
local char = script.Parent
local hrp = script.Parent:WaitForChild("HumanoidRootPart")
print(hrp.Position)
local runservice = game:GetService("RunService")
local contextActionService = game:GetService("ContextActionService")
local targetlock = game:GetService("ReplicatedStorage"):WaitForChild("LockOn")
local targeting = script.Targeting
local targets = {}
local lockon = targetlock:Clone()
local bb = lockon:WaitForChild("bb")
local TweenService = game:GetService("TweenService")
local rep = game:GetService("ReplicatedStorage")
local info = TweenInfo.new(
	0.1,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.In,
	0,
	false,
	0
)
local function findTarget(actionName, inputState, _inputObject)
	if targeting.Value == false and inputState == Enum.UserInputState.Begin then
		print(targets)
		targeting.Value = true
		distance = 40
		local count = 0
		for i, v in pairs(workspace:GetChildren()) do
			if v:IsA("Model") then
				if v:FindFirstChild("HumanoidRootPart") then
					local mag = (v.HumanoidRootPart.Position - hrp.Position).Magnitude
					if mag < distance and v ~= script.Parent then
						local Focus = v.HumanoidRootPart
						print(Focus.Parent)
						table.insert(targets, Focus)
					end
				end
			end
		end
	if #targets ~=0 then
		lockon.Parent = workspace
		lockon.Position = targets[1].Position
	end
	elseif targeting.Value == true and inputState == Enum.UserInputState.Begin then
		table.clear(targets)
		targeting.Value = false
		camera.CameraType = Enum.CameraType.Custom
		lockon.Parent = rep
	end
end
contextActionService:BindAction("Target", findTarget, true, Enum.KeyCode.T)
contextActionService:SetTitle("Target", "Target Lock")
target = 1
local cycle = coroutine.create(function()
	local function cycle(actionName, inputState, _inputObject)
		if #targets ~= 0 and inputState == Enum.UserInputState.Begin then
			if target < #targets then
				target = target + 1
				local goal1 = {Position = targets[target].Position}
				local anim1 = TweenService:Create(lockon, info, goal1)
				anim1:Play()
			else
				target = 1
				local goal1 = {Position = targets[target].Position}
				local anim1 = TweenService:Create(lockon, info, goal1)
				anim1:Play()
			end
		end
	print(target)
	end
	targeting.Changed:Connect(function()
		if targeting.Value == true then
			contextActionService:BindAction("Cycle", cycle, true, Enum.KeyCode.Q)
			contextActionService:SetTitle("Cycle", "Cycle")
		else
			table.clear(targets)
			contextActionService:UnbindAction("Cycle")
			lockon.Parent = rep
			camera.CameraType = Enum.CameraType.Custom
		end
	end)
end)
coroutine.resume(cycle)
runservice.RenderStepped:Connect(function()
	if targeting.Value == true and #targets ~= 0 then
		if targets[target] ~= nil and (targets[target].Position - hrp.Position).Magnitude >= distance + 10 then
			targeting.Value = false
			camera.CameraType = Enum.CameraType.Custom
			lockon.Parent = rep
		end
	if targets[target] ~= nil then
			hrp.CFrame = CFrame.new(hrp.Position, Vector3.new(targets[target].Position.X,hrp.Position.Y,targets[target].Position.Z))
			camera.CFrame = hrp.CFrame*CFrame.new(Vector3.new(3,2.5,10))
			camera.CameraType = Enum.CameraType.Scriptable
	else
		lockon.Parent = rep
	end
	if targeting == false then
		table.clear(targets)
		lockon.Parent = rep
	end
	end
end)


1 Like