Trying to get a Zelda like Z Targeting thing working but everything I've tried has failed lol

Here’s the code I’ve got currently.

I literally have no idea what’s wrong, no errors, I have it as a modulescript, got it in serverscriptservices etc, any ideas?


local Players = game:GetService("Players")

local Target = nil

local function pickTarget()
	local Mouse = Players.LocalPlayer:GetMouse()
	local Character = Players.LocalPlaye.Character
	if not (Mouse and Character) then return end

	local MaxDistance = 50 --change this to adjust targeting range
	local Origin = Character.HumanoidRootPart.Position
	local Direction = (Mouse.Hit.Position - Origin).Unit
	local part, hitpos = workspace:FindPartOnRayWithWhitelist(Ray.new(Origin, Direction * MaxDistance),{Character})
	while part and part.CanCollide == false do
		part, hitpos = workspace:FindPartOnRayWithIgnoreList(Ray.new(hitpos, Direction * MaxDistance), {Character})
	end

	Target = (part and part.Parent and part.Parent:FindFirstChild("Humanoid") and part.Parent.Humanoid or nil)
end

local function getRotation(part)
	if not part then return nil end
	if not part:IsA("BasePart") then return nil end

	local camCFrame = workspace.CurrentCamera.CFrame
	local look = (camCFrame.p - part.Position).Unit
	local up = part.CFrame.UpVector

	local right = up:Cross(look).unit
	local newUp = look:Cross(right)

	return CFrame.fromMatrix(part.Position, right, newUp)
end

local function updateTargeting()
	local Character = Players.LocalPlayer.Character
	if not (Character and Target) then return end

	local TargetPart = Target.Parent.PrimaryPart
	if not TargetPart then TargetPart = Target.Parent:FindFirstChildWhichIsA("BasePart") end
	if not TargetPart then return end

	local CurrentRotation = getRotation(Character.HumanoidRootPart)
	local TargetRotation = getRotation(TargetPart)
	if not (CurrentRotation and TargetRotation) then return end

	local AngleBetween = math.acos(CurrentRotation.LookVector:Dot(TargetRotation.LookVector))
	if AngleBetween > math.rad(20) then return end -- change this to adjust lock onm angle

	Character.Humanoid.AutoRotate = false
	Character.HumanoidRootPart.CFrame = CFrame.new(Character.HumanoidRootPart.Position, TargetPart.Position)
end

game:GetService("RunService").RenderStepped:Connect(function()
	pickTarget()
	updateTargeting()
end)
1 Like

Hello sir why is some of the code not embedded its making it kinda difficult to read :slight_smile:

1 Like

Not sure exactly why it did that

1 Like

Fixed it, I apologize for the inconvenience.

1 Like

if its meant to be local which it is i assume, it wont run if it’s in serverscriptservice

1 Like

Ahh I assumed it was supposed to be a modulescript.

1 Like

i see u could probably have it in replicatedfirst or starterplayerscripts

1 Like

Thank you I’m going to try out both.

1 Like

Neither of them seem to work, very odd wait let me read line 7 and 55 real quick it’s saying there is an error.

I’m dead I think it’s a spelling error…

1 Like

alr at least its giving errors now :smiley:

always love tiny bugs liek that

1 Like

Ok so I scrapped the other one and went for another one, but now this one is getting an error on line 49 saying that it’s attempting to suffix nil for the humanoid, not sure what’s going on with this one lmbo.

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local InputService = game:GetService("UserInputService")
local Camera = workspace.CurrentCamera

local target = nil
local targetModel = Instance.new("Model", workspace)
targetModel.Name = "Target Model"
local targetPart = Instance.new("Part")
targetPart.Size = Vector3.new(2,2,2)
targetPart.Transparency = 0.7
targetPart.BrickColor = BrickColor.new("Institutional white")
targetPart.CanCollide = false
targetPart.Anchored = true
targetPart.Parent = targetModel

local function findTarget()
	local nearestDistance = math.huge
	for _,player in pairs(Players:GetPlayers())do
		if player ~= LocalPlayer and player.Character and player.Character:FindFirstChild("HumanoidRootPart") and player.Character:FindFirstChild("Humanoid") and player.Character.Humanoid.Health>0 then
			local distance = (player.Character.HumanoidRootPart.Position - LocalPlayer.Character.HumanoidRootPart.Position).magnitude
			if distance < nearestDistance then
				nearestDistance = distance
				target = player.Character.HumanoidRootPart
			end
		end
	end
end

local function onInput(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		if input.UserInputState == Enum.UserInputState.Begin then --Player has pressed shift
			findTarget()
		elseif input.UserInputState == Enum.UserInputState.End then --Player has stopped pressing shift
			target = nil
		end
	end
end

InputService.InputChanged:Connect(onInput)

while true do
	if target then
		local targetPosition = target.Position + Vector3.new(0, targetPart.SizeY/2,0)
		targetPart.CFrame = CFrame.new(targetPosition)
		Camera.CameraSubject = target
		Camera.CameraType = Enum.CameraType.Attach
	else
		Camera.CameraSubject = LocalPlayer.Character.Humanoid
		Camera.CameraType = Enum.CameraType.Custom
	end
	wait(0.1)
end

Not sure if I have it right on this one either, I have this one as a localscript and I put it in both the ReplicatedFirst and the StarterScripts.

The other one I had “worked” but it was so janky I had to scrap it.

i think camerasubject has to be a part

edit nvm just checked

maybe the humanoid isnt loaded in yet try WaitForChild(“Humanoid”)

1 Like

Where would I add it exactly my friend still new to this and I don’t want to just inject lines randomly lol.

1 Like

sorry i went offline, at line 49
Camera.CameraSubject = LocalPlayer.Character:WaitForChild(“Humanoid”)

2 Likes

Try:


local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local InputService = game:GetService("UserInputService")
local Camera = workspace.CurrentCamera

local target = nil
local targetModel = Instance.new("Model", workspace)
targetModel.Name = "Target Model"
local targetPart = Instance.new("Part")
targetPart.Size = Vector3.new(2,2,2)
targetPart.Transparency = 0.7
targetPart.BrickColor = BrickColor.new("Institutional white")
targetPart.CanCollide = false
targetPart.Anchored = true
targetPart.Parent = targetModel

local function findTarget()
	local nearestDistance = math.huge
	for _,player in pairs(Players:GetPlayers())do
		if player ~= LocalPlayer and player.Character and player.Character:FindFirstChild("HumanoidRootPart") and player.Character:FindFirstChild("Humanoid") and player.Character.Humanoid.Health>0 then
			local distance = (player.Character.HumanoidRootPart.Position - LocalPlayer.Character.HumanoidRootPart.Position).magnitude
			if distance < nearestDistance then
				nearestDistance = distance
				target = player.Character.HumanoidRootPart
			end
		end
	end
end

local function onInput(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		if input.UserInputState == Enum.UserInputState.Begin then --Player has pressed shift
			findTarget()
		elseif input.UserInputState == Enum.UserInputState.End then --Player has stopped pressing shift
			target = nil
		end
	end
end

InputService.InputChanged:Connect(onInput)

while true do
	if target then
		local targetPosition = target.Position + Vector3.new(0, targetPart.SizeY/2,0)
		targetPart.CFrame = CFrame.new(targetPosition)
		Camera.CameraSubject = target
		Camera.CameraType = Enum.CameraType.Attach
	else
		Camera.CameraSubject = LocalPlayer.Character:WaitForChild(“Humanoid”)
		Camera.CameraType = Enum.CameraType.Custom
	end
	wait(0.1)
end

1 Like