-- // Connections

playersService.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        
        local rootPart = character:WaitForChild("HumanoidRootPart")
        
        local humanoid = character:WaitForChild("Humanoid")
        local oldHealth = humanoid.Health
        
        local particlesClone = bloodParticles:Clone()
        particlesClone.Parent = rootPart
        
        humanoid.HealthChanged:Connect(function(health)
            if health < oldHealth then
                
                oldHealth = health
                
                spawn(function()
                    for i = 1, blood_parts_per_hit do
                        
                        local raycastOrigin = rootPart.Position + Vector3.new(math.random(-20, 20) / 10, 0, math.random(-20, 20) / 10)
                        local raycastDirection = Vector3.new(0, -8, 0)
                        
                        local raycastResult = raycast(character, raycastOrigin, raycastDirection)
                        
                        if raycastResult then
                            particlesClone:Emit(2)
                            
                            delay(0.5, function()
                                local newBlood = createBlood(raycastResult.Position)
                                expandBlood(newBlood)
                                
                                delay(blood_destroy_delay, function()
                                    deleteBlood(newBlood)
                                end)
                            end)
                        end
                        
                        wait(math.random(0.5, 2) / 10)
                    end
                end)
            else
                oldHealth = health
            end
        end)
        
    end)
end)
