You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I need to get the Humanoid from a Player.
What is the issue? Include screenshots / videos if possible! 44: attempt to index nil with ‘Humanoid’
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
Target is a HumanoidRootPart.
function main()
local target = findTarget()
if target then
local char = target.Parent:FindFirstChild("Character")
local human = char.Humanoid -- HERE ERROR
if human and human.MoveDirection.Magnitude > 0 then
target.Parent.Humanoid.Health -= 5
end
else
print("hided")
end
end
means the character doesnt exists.
looking at your script, you are making a kind of gun?
char is equal to nil, so target.Parent has no children named “Character.”
i think this may work for you:
function main()
local target = findTarget()
if target then
local char = target.Parent:FindFirstChild("Character")
if (not char) or (not char:FindFirstChild("Humanoid")) then return end;
local human = char.Humanoid -- HERE ERROR
if human and human.MoveDirection.Magnitude > 0 then
target.Parent.Humanoid.Health -= 5
end
else
print("hided")
end
end
function main()
local target = findTarget()
if target then
local human = target:FindFirstChild('Humanoid') -- HERE ERROR
if human and human.MoveDirection.Magnitude > 0 then
human.Health -= 5
end
human = nil
else
print("hided")
end
end
The above assumes that the target is a character. If the target is a player, do this instead:
function main()
local target = findTarget()
if target then
local human = target.Character:FindFirstChild('Humanoid') -- HERE ERROR
if human and human.MoveDirection.Magnitude > 0 then
human.Health -= 5
end
human = nil
else
print("hided")
end
end
function main()
local target = findTarget()
if target then
local human = target.Parent:FindFirstChildOfClass("Humanoid")
if human and human.MoveDirection.Magnitude > 0 then
target.Parent.Humanoid.Health -= 5
end
else
print("hided")
end
end
function main()
local target = findTarget()
if target then
local human = target.Parent:FindFirstChildOfClass("Humanoid")
if human and human.MoveDirection.Magnitude > 0 then
human.Health -= 5
end
else
print("hided")
end
end
local character = script.Parent
character:WaitForChild('Humanoid')
local name = {}
local class = {}
for i=1,1000 do
local start = os.clock()
character:FindFirstChild('Humanoid')
table.insert(name,os.clock()-start)
start = nil
end
for i=1,1000 do
local start = os.clock()
character:FindFirstChildOfClass('Humanoid')
table.insert(class,os.clock()-start)
start = nil
end
local nameAverage = 0
for _,n in name do
nameAverage+=n
end
nameAverage/=#name
local classAverage = 0
for _,n in class do
classAverage+=n
end
classAverage/=#class
print('Name: '..nameAverage)
print('Class: '..classAverage)
I actually find that name is faster. But it doesn’t really matter which you use.
well yes i dont mind people who use these methods, but FindFirstChildOfClass only searches instances by their classnames. so it’s 2 different functions
That’s true, each have their own use. But the reason using the name is faster is because the name is a direct index, where as to get the class, it has to iterate through every child to compare their ClassName. In addition to that, FindFirstChild also has a second optional argument which would, if set to true, iterate through all descendants, instead just indexing by name.
So unless you’re using other things within the character that would have the name of Humanoid, I believe FindFirstChild is better.