I’m working on a weapon class for a game, and am trying to get the HumanoidRootPart of the hit humanoid and the attackers humanoid. When I print out these they work fine, but when I use findfirstchild to index them it throws nil? Apologies if this post is confusing I’m super tired writing this
function Gauntlet:OnHit(humanoid, damage)
if self._hitdebounce then return end
if humanoid.Parent.RagdollTrigger.Value == true then return end
if humanoid.Parent.Parent:FindFirstChildOfClass("ForceField") then return end
print(humanoid)
print(humanoid.Parent)
local root = humanoid.Parent:FindFirstChild("HumanoidRootPart")
local myRoot = self.character:FindFirstChild("HumanoidRootPart")
self._hitdebounce = true
humanoid:TakeDamage(damage)
if root and not root.Anchored and myRoot then
local delta = root.Position - myRoot.Position
root:SetNetworkOwner(nil)
humanoid.Parent.RagdollTrigger.Value = true
root:ApplyImpulse(delta.unit * self.config.KNOCKBACK_STRENGTH_X + Vector3.new(0, self.config.KNOCKBACK_STRENGTH_Y, 0))
end
task.wait(2)
self._hitdebounce = false
end
Are you sure the error is coming from humanoid.Parent:FindFirstChild? Looks to me like you do not check self.character and call FindFirstChild on that as well.
Try using WaitForChild(), that might work
This will not work if the error is attempt to index nil with FindFirstChild. That means that you are running the equivalent of nil:FindFirstChild(something).
I tried printing self.character and it prints nil, I don’t understand cause I use it in my constructor function and it works fine, I guess other functions aren’t able to index the character that’s used in the constructor function? I don’t understand. I’m finding the character of the tool using this:
local function findCharacter(path)
local plr = path.Parent.Parent
local char = plr.Character or plr.CharacterAdded:Wait()
return char
end
Should I just use this again instead of referencing the one used in my constructor function?
I’m kind of confused, why wouldn’t the character exist? I get self.character from the tools parent, which should always be a character. Is there a better way to do this?
local gauntlet = Gauntlet.new(tool, character)
gauntlet:OnHit(humanoid, damage) -- will work!
Gauntlet:OnHit(humanoid, damage) -- using the class, will not work!
Is self in your case the gauntlet? If so you can try self:OnHit(hitHum, self.config.DAMAGE).