So im trying to make this script that locks on to a player if you hover your mouse over them and then press x, however im having troubles actually finding the targets as local Human = Characterc:FindFirstChild("Humanoid")
always returns nil anybody have any idea why?
Code (Local Script in starter player scripts)
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local RunService = game:GetService("RunService")
local ContextActionService = game:GetService("ContextActionService")
local Players = game:GetService("Players")
local Mouse = Player:GetMouse()
local Target = nil
local TargetHuman = nil
Player.CharacterAdded:Connect(function(c)
print("Updating self")
Character = c
Humanoid = Character:WaitForChild("Humanoid")
end)
function Rotate()
if Humanoid ~= nil and Humanoid.Health > 0 and Target ~= nil and TargetHuman ~= nil then
if TargetHuman.Health > 0 then
print("Rotating")
local newpos = Vector3.new(Target.HumanoidRootPart.CFrame.X, Character.HumanoidRootPart.CFrame.Y, Target.HumanoidRootPart.CFrame.Z)
local faceto = CFrame.new(Character.HumanoidRootPart.Position, newpos)
Character:SetPrimaryPartCFrame(faceto)
else
print("Removing Target")
Target = nil
TargetHuman = nil
end
else
print("Removing Target")
Target = nil
TargetHuman = nil
end
end
function CheckTarget()
print("Checking Targets")
if Mouse.Target ~= nil then
local Characterc = Players:GetPlayerFromCharacter(Mouse.Target.Parent)
if Characterc then
print("Got character")
local Human = Characterc:FindFirstChild("Humanoid")
print(Human)
if Human then
print("Target Found")
Target = Characterc
TargetHuman = Human
end
end
else
print("Nil")
end
end
print("Binding Functions")
ContextActionService:BindAction("LockOn", CheckTarget, false, Enum.KeyCode.X)
RunService:BindToRenderStep("rotate", Enum.RenderPriority.Last.Value, Rotate)