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)
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
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.