[SOLVED] Humanoid is not a valid member of Workspace "Workspace"

So, i’m trying to make a RPG game, and there’s a sword (ROBLOX free model sword) that gives you the stats. Since I need to detect if I slayed an enemy, I need their humanoid, which leads to the error mentioned in the title.

local db = false

Tool.Activated:Connect(function()
	Tool.Handle.Touched:Connect(function(hit)
		local mult = math.random(1,10)
		local mob = mult*(math.random(1,12))
		if hit == nil or hit.Parent == nil then return end
			if hit.Parent.Humanoid.Health == 0 then -- line related to error
			if db == false then
				game.Players[Tool.Parent.Name].leaderstats.KO.Value += 1
				game.Players[Tool.Parent.Name].leaderstats.Gold.Value += mob
				db = true
				wait(.5)
				db = false
			else
				
			end
		end
	end)
end)

Thanks

That’s because you’re likely colliding with a Part that is a member of Workspace and not a character on .Touched.

-- On this line you can add a check to make sure you collide with character containing a humanoid.
if hit == nil or hit.Parent == nil or not hit.Parent:FindFirstChild('Humanoid') then return end

Doesn’t seem to spit out errors! Thanks!
Should I remove the hit == nil or hit.Parent == nil tho?

(so the code wouldnt be complex)

You don’t need to, although (very) unlikely that you hit something that is in the process of being destroyed, it’s possible that they stop future errors.

1 Like

“hit” can never be nil. hit.Parent can, but only if the part touched your part the milliseconds it was being destroyed.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.