How come this happens after player hits enemy?

I’m working on a new blood particle system for my gun system, and I decided to work on implementing it to melee first. For some reason, the blood system works exactly once before it shows an error in output that I don’t seem to understand or know how to fix. Help?

Code:

local Tool = script.Parent

local Handle = Tool:WaitForChild("Handle")

local MeleeDamageActivate = script.Parent.Events.MeleeDamageEvent

CanDamage = false

CanSwing = true

local DebrisService = game:GetService('Debris')

local ReplicatedStorage = game.ReplicatedStorage

local DMGParticle1 = ReplicatedStorage.PlayerStuff.DMGParticle1:Clone()
local DMGParticle2 = ReplicatedStorage.PlayerStuff.DMGParticle2:Clone()

MeleeDamageActivate.OnServerEvent:Connect(function()  
	function slash()
		print("Swing")
		
		if CanSwing == true then
		CanSwing = false

		wait(0.15)
		CanDamage = true
		Handle.MeleeSwing:Play()
		
		wait(0.4)  -- delay for swinging
		CanDamage = false
		
		wait(0.06)
		CanSwing = true
		end
	end
	slash()
end)

function HumanoidHit(Touch)
	local Humanoid = Touch.Parent:FindFirstChildOfClass("Humanoid")
	
	if Humanoid ~= nil and CanDamage == true then
		CanDamage = false
		
		DMGParticle1.Parent = Touch.Parent.UpperTorso
		DMGParticle2.Parent = Touch.Parent.UpperTorso
		
		DMGParticle1.Rate = 30
		DMGParticle2.Rate = 30
		
		DebrisService:AddItem(DMGParticle1, 0.4)
		DebrisService:AddItem(DMGParticle2, 0.4)
		
		local SoundPart = Humanoid.Parent.SoundPart
		
		Humanoid:TakeDamage(Tool.Values.MeleeDamage.Value) -- deals damage to target
		
		local randomnumber = math.random(1, 2) -- random hit sound play

		if randomnumber  == 2 then 
		SoundPart.MeleeHit2:Play()
		end	
		if randomnumber == 1 then 
		SoundPart.MeleeHit1:Play()	
		end

		
	end
end

script.Parent.BashHitbox.Touched:Connect(HumanoidHit) -- tells what part that is touched by the target to damage

Error:

1 Like

Check the property it’s probably locked.

I believe it is because you only have 1 copy of it. You should be cloning that value inside your HumanoidHit routine because the Debris will destroy the only one you have created and the second time in it is NULL.

So move these 2 lines after your CanDamage = false in HumanoidHit:

local DMGParticle1 = ReplicatedStorage.PlayerStuff.DMGParticle1:Clone()
local DMGParticle2 = ReplicatedStorage.PlayerStuff.DMGParticle2:Clone()

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