I’ve been having an issue with my game where whenever you hit an enemy with your weapon, there’s a chance of the enemy randomly disappearing. I’m not really sure what’s causing it, but I think it has to do with my damage script.
video of the issue:
If I had to guess, the issue could be with either
a) some issue with line 22 where I set up ragdoll knockback distance
b) some issue in line 18, which has been giving me an error I don’t know how to fix reading, “Players.playerpack12345.Backpack.Bat.Damage:18: attempt to index nil with ‘FindFirstChild’”.
Why is this issue happening and how can I fix 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,part)
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,0,60) + Vector3.new(0,40))
ragdollmodule.Start(humanoid.Parent)
for i in hitlist do
hitsound:Play()
end
wait(1.5)
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
(im super new to scripting so forgive me if i dont understand something)