Need help finding + fixing bug with weapon script making enemies disappear


there is a bug in my code that causes enemies to disappear. this bug seems to happen randomly and i cant figure out exactly what the issue is. however, ive been able to narrow it down to either a certain line (line 20) in the damage script, or the module script which controls how knockback works. what is causing the issue and how do i go about fixing it?

damage script:

local tool = script.Parent
local bat = tool.BatModel
local damage_re = tool:WaitForChild("SwingEvent")
local hitsound = bat.BatHit
local ragdollmodule = require(game:GetService("ServerScriptService").Ragdoll)
local kbmodule = require(game:GetService("ServerScriptService").Knockback)

local hitbox = Instance.new("Part",tool)

local connection = nil --we technically don't need this anymore, but since we set something to it later we have to include it or stop using it

local hitlist = {}

local function inflict_damage(other_object)
	if(not hitlist[other_object.Parent]) then --check if the parent has already been seen
		local humanoid = other_object.Parent:FindFirstChild("Humanoid")
		if humanoid then
			hitlist[other_object.Parent] = true --This can be anything other than nil or false.  It's the value we get back when we ask what is inside hitlist[other_object.Parent].  It will be nil if it doesn't exist, but will be true in this case if it does exist in the hitlist.
			humanoid:TakeDamage(25)
			kbmodule.Start(humanoid.Parent, (humanoid.Parent.HumanoidRootPart.Position - other_object.Position).Unit * Vector3.new(60,40,60) + Vector3.new(0,40))
			ragdollmodule.Start(humanoid.Parent)
			for i in hitlist do
				hitsound:Play()
			end
			wait(2)
			ragdollmodule.Stop(humanoid.Parent)
		end
		--We don't disconnect here anymore.  If we disconnect, then we will only run this function on the first block we hit, but we want it to run on all of them.
	end
end

damage_re.OnServerEvent:Connect(function()
	local hitbox = Instance.new("Part",tool)
	hitbox.Size = Vector3.new(1,5,1)
	hitbox.CanCollide = false
	hitbox.Massless = true
	hitbox.Transparency = 0.7 --debug

	local weld = Instance.new("Weld",hitbox)
	weld.Part0 = bat
	weld.Part1 = hitbox
	game.Debris:AddItem(hitbox,0.4)

	hitlist = {} --resets hitlist by creating a new list
	
	connection = hitbox.Touched:Connect(inflict_damage)
end)

--i got this script on youtube: https://www.youtube.com/watch?v=kb0zcUA7OMg&ab_channel=ManlyCreator
--any changes from the original were made by me and mostly this dude tlr22 on the devforum so props 2 him

knockback module script:

local kb = {}

function kb.Start(character,direction)
	local hrp = character:FindFirstChild("HumanoidRootPart")
	
	if hrp == nil then return end
	
	local att = Instance.new("Attachment",hrp)
	local lv = Instance.new("LinearVelocity",att)
	lv.MaxForce = 9999999
	lv.VectorVelocity = direction
	lv.Attachment0 = att
	game.Debris:AddItem(att,0.1)
end

return kb

(im super new to scripting so please forgive me if i dont understand something)

1 Like