Humanoid is not a valid member of Model "Dummy", trying to stop this with checks but its not really working

basically trying to make a damage over time talent, which is added ontop of normal gun damage but ive tried to get it to stop erroring, because when it does the bullets get stuck on the dummy and just causes hell

local X = 0
local module = {}

module.Yes = function(hit, strings, bullet)
	local tool = strings.Parent
	local gt = strings.GunType
	local BaseGun = require(game:GetService("ReplicatedStorage").Damage:FindFirstChild(gt.Value))
	if strings.GunLevel.Value > strings.ReqT2.Value then
		local index = math.random(1,5)
		if index ~= 3 then
			return
		end
		local C2 = hit.Parent
		if not C2 then
			return
		end
		local DmgV = tool.strings.Damage
		local gLevel = tool.strings.GunLevel
		local character = hit:FindFirstAncestorWhichIsA("Model")
		local C = character.HumanoidRootPart
		if C:FindFirstChild("Fire") then
			return
		else
			local fire = script.Fire:Clone()
			fire.Parent = C
			bullet.Transparency = 1
			bullet.PointLight.Enabled = false
			repeat
				if not C2 then
					task.cancel()
				else
					character.Humanoid:TakeDamage((math.round((DmgV.Value * (gLevel.Value/10))*0.2)))
					print((math.round((DmgV.Value * (gLevel.Value/10))*0.2)))
					X += 1
					task.wait(1)					
				end
			until X == 5
			X = 0
			bullet.Transparency = 0
			bullet.PointLight.Enabled = true
			fire:Destroy()
		end
	end
end

return module

the errors only ever come from when i start the repeat

It errors because humanoid does not appear instantly, It only appears at milliseconds so change character.Humanoid to character:WaitForChild('Humanoid')

1 Like

I’m not sure whether this helps, but…

You’re using task.cancel incorrectly, I suppose. I think you want the repeat loop to stop, but a simple return can stop the entire function, or break to stop the current repeat loop. If you actually have to use the function though, you’ll need a thread.

You use task.cancel to cancel a running thread made using task.delay, task.spawn, and some other task functions (idk what other functions make a new thread. i only use task for task.wait)